0

Suppose we have a string:

some random text $$ hello world $$ some more stuff

In the string delimited by $$, I would like to capture all occurrences of o (for example, in order to find and replace in Sublime Text 3).

How do I formulate such a regex command? Even though I plan to use this regex command in Sublime Text, I can take a regex command that uses different notation and fix it for use in ST3.

samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
bzm3r
  • 3,113
  • 6
  • 34
  • 67
  • It is impossible to solve it with a regex inside a text editor in one go. You would need to do this in three steps: 1) replace each pair of `$$` with non-identical temporary delimiters, 2) replace each `o` inside these temp delimiters, 3) restore the original `$$`s. – Wiktor Stribiżew Nov 14 '19 at 08:11
  • Also, it looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/a/2759417/3832970) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Nov 14 '19 at 08:13
  • @WiktorStribiżew I have used regex quite a bit, particularly with the help of: https://www.regular-expressions.info/ – bzm3r Nov 14 '19 at 09:07

2 Answers2

1

Use the regex:

(\$\$[^o]*)o(.*?\$\$)

replace with:

\15\2

where 5 is the substitution string.

Test here.


If you want to search for a sub-string instead of only one character, you may use:

(\$\$.*?)lo(.*?\$\$)

with the same replacement string.

Test here.


Another option is to extract first the sub-string between the delimiters ($$ hello world $$), and then on the result perform any search-and-replace action you need. This might also need looping until no replacements are done any more.

virolino
  • 2,073
  • 5
  • 21
  • 2
    this will only replace the first occurrence of the letter "o" in the string. The "o" in hello but not the "o" in world. – samuelbrody1249 Nov 14 '19 at 05:21
  • 2
    "Repeatedly capture" can also mean that you call the replacement function as many times as needed, until no replacements are done. I think that what you want cannot be done easily in only one step anyway. – virolino Nov 14 '19 at 05:26
  • right, actually I'm not sure the above can be done in a single regex either (I've posted my answer below), but I'm no expert in regex by any means so I'd be interested to see if it's possible. – samuelbrody1249 Nov 14 '19 at 05:29
1

To the best of my regex knowledge, you'll need to use two regular expressions to replace all the o's between the delimiters. The first one would be to actually grab the text within the delimiter. For example:

(?P<start>\$\$)        # start by grabbing the starting literal '$$'
(?<middle>.*?)(?=\$\$) # then grab everything up until the ending '$$'
(?P<end>\$\$)          # now grab the ending '$$'

Example: https://regex101.com/r/015Xj8/4

Obviously you know what the start and ending is so you can simplify it further if you wanted (no start or end group for example), but I've included that for thoroughness.

Once you've captured the start/end of it, you can replace the o by a straight find/replace on the literal 'o'. As far as I know, it takes two steps via regex to do what you're after. I'm not too knowledgeable about sublime but perhaps there's a sed-like replacement feature in it.

The following will replace the first o in the string (similar to the other answer by virolino), but you'll need to click "Enter" a bunch of times to make sure that all the last o's are replaced for this to be useful:

(?P<start>\$\$)
(?<middle>.*?(?<first_o>o).*?)(?=\$\$)
(?P<end>\$\$)

Or, if you're just looking to capture O's (and nothing else), you can just make sure it's between a starting and ending $$:

\$\$.*?(o).*?\$\$
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58