-2

Take the sample string:

__________Hello

I want to replace lines starting with 10 x _ with 20 x _

Desired output:

____________________Hello

I can do this a number of ways, i.e:

/^(_{10})/\1\1/
/^_{10}/____________________/
/^(__________)/\1\1/
etc...

Question:

Is there a way within the regex specification/expression itself - say PCRE (or any regex library/engine for that matter) - to specify the replacement occurence of a character ?

For example:

/_{10}/_{20}/

I don't know if I'm having a mind blank or if I've just never done this, but I cannot seem to find any such thing in the regex specification docs.

hmedia1
  • 5,552
  • 2
  • 22
  • 27
  • No, and there is no need for that, there are other ways to achieve the same. E.g. with callback methods used as replacement arguments. – Wiktor Stribiżew Jun 25 '21 at 10:03
  • No regex flavor allows to "specify the replacement occurence of a character". – Wiktor Stribiżew Jun 25 '21 at 10:22
  • @WiktorStribiżew I omitted the fact that I can handle it programmatically to be more concise. Respectfully, it's not for you to say I don't *need* it. By that Logic there is much we don't need, but in my specific case, yes that would be the desirable outcome. – hmedia1 Jun 25 '21 at 10:23
  • If you can do that with code, you do not need that functionality. That is missing anyway. – Wiktor Stribiżew Jun 25 '21 at 10:24
  • 1
    @WiktorStribiżew Again not true. You don't know my context at all. It's surprising you are insistent on what you don't know. And seriously, what's with the DV and close vote. Legitimate and non-duplicate question, formatted well and and very clear. Fine candidate for QA that could simply be answered and added for the value of anyone else looking for it later. – hmedia1 Jun 25 '21 at 10:26
  • 1
    "You don't know my context at all." - Where did you explain your "context"? What don't I know? I know the feature you ask for does not exist. I answered your question in the first two comments. The fact you provided a ton of solutions for you own problem proves there is no real issue - no issue, no need for a question. – Wiktor Stribiżew Jun 25 '21 at 10:34
  • @WiktorStribiżew I appreciate the confirmation. A fine answer would have been your second comment alone. Though not required or obligated - Even better would have been that with some additional insight on why one should not expect such a thing. None the less, a terrible answer is to simply tell me that I don't actually need the thing I'm asking about. I'm not missing my code, it would be irrelevant bloat that serves no purpose. I provided trivial input and desired output, and clarified that I'm looking for something that exists in the regex itself. If it doesn't exist, then that's fine – hmedia1 Jun 25 '21 at 11:10
  • 1
    **Duplicate of [Can quantifiers be used in regex replacement in R?](https://stackoverflow.com/questions/64594677/can-quantifiers-be-used-in-regex-replacement-in-r)** You can even find a PCRE regex example there. – Wiktor Stribiżew Jun 25 '21 at 11:18
  • @WiktorStribiżew Thanks. I was actually just doing some manual text manipulation in an editor, so I wanted to see if this was achievable without writing a plugin. – hmedia1 Jun 25 '21 at 12:34
  • @WiktorStribiżew It's not a duplicate of an R programming question. Please delete that link. – hmedia1 Jun 27 '21 at 13:40
  • In R, one of the supported regex flavors is PCRE, and in that post, exactly the PCRE pattern is suggested and explained, it is exactly the answer here ("No such feature is available") – Wiktor Stribiżew Jun 27 '21 at 19:06
  • @WiktorStribiżew You just confirmed it's not a duplicate. "No such feature is available", that's fine. That doesn't make my question a duplicate of an R problem – hmedia1 Jun 28 '21 at 14:30
  • The first line in my answer explains it for **any regex flavor**: "*Quantifiers cannot be used in the replacement pattern, nor the information how many chars they match.*" – Wiktor Stribiżew Jun 28 '21 at 15:52
  • @WiktorStribiżew That's perfect. Appreciate that. It's still not an R question, or an R answer. – hmedia1 Jun 28 '21 at 17:43
  • R uses several regex flavors, PCRE is one of them. The post I link to is about PCRE regex flavor. – Wiktor Stribiżew Jun 28 '21 at 17:44
  • 1
    @WiktorStribiżew I see what you're saying. However, correct me if I'm wrong. That question is seeking to quantify substitutions for what is already there on the input. i.e. "*How should I replace the exact number of characters in \\S* by dots or any other symbol?*" - I was looking to specify a *different* number of replacements to what was matched in the input. – hmedia1 Jun 28 '21 at 18:15

1 Answers1

0

It can't be done within the Regex itself.

If I have the input "39572a4872" and I want to replace it with "39572aaaaa4872", there are many simple ways to achieve that, which can include Regular expressions, but as Wiktor explained in the comment thread, the actual quantifier of the replacement is not something itself that is achieved through regex.

It may seem unimportant, since in this example I could simply just apply the replacement 5 times manually or programatically, but one of the benefits of standardized technologies is applying the same concepts in different environments, languages, even within programs.

I as well as many others have had a lot of success with the portability of my regex because of this.

This question was to see if specifying quantifiers for replacement strings was possible within the syntax of a regex itself. Which it is surely not.

hmedia1
  • 5,552
  • 2
  • 22
  • 27