0

How can I replace 2 known strings separated by an unknown string using a regexp?

For example, I might have

known_string_1 blah_random text blah known string 2

I know I need some sort of wildcard expression between the two known strings in the refexp, but being a regexp nooblet I have no idea what to use. The unknown string in the middle of the two known strings could be any length.

I'm using this to replace some old code with new stuff, but the fact that known blocks are indented with varying tabs doesn't help.

Thanks a lot,

James

Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • 1
    /nooblet, what have you tried? Give it a shot at least; it will help the learning process. http://www.regular-expressions.info/reference.html http://www.weitz.de/regex-coach/ – Brad Oct 13 '10 at 16:24
  • @Brad I should have tried first really. Thanks for the advice - I admit posting here was a trifle lazy :) – Bojangles Oct 13 '10 at 16:26

2 Answers2

3

Very simply, .* will match any character, any number of times.

So for your situation here, the regex

known_string_1.*known_string_2

should work so long as none of the characters in your known strings are themselves metacharacters such as ?, +, etc. In which case they would need to be escaped with a \ such as \?, \+, etc.

Austin Philp
  • 94
  • 1
  • 10
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
1

Using .* as the pattern for the unknown text between the two known strings will get you most of the way. However, what if you have a string that's like known_string_1 unknown_text_1 known_string_2 unknown_text_2 known_string_2?

If you just use .*, then this will match greedily, and it will match the string unknown_text_1 known_string_2 unknown_text_2. Is this what you want?

If that's not what you want (i.e. you just want to remove unknown_text_1) then you need to use the non-greedy modifier: .*?.

And as an aside, I hope that your known_text_1 and known_text_2 strings aren't opening and closing [X]HTML elements. Everybody knows you shouldn't parse [X]HTML with a regular expression.

Community
  • 1
  • 1
CanSpice
  • 34,814
  • 10
  • 72
  • 86
  • What I'm doing is replacing old HTML in some files with updated stuff (replace ugly table code with nice div code). This regex is only going to be used with a GUI regex text replacement tool, not a script which is what you're thinking I'm using (As an aside... bit). As for the greediness, it doesn't matter - known_string_2 only appears once, but thanks all the same! – Bojangles Oct 14 '10 at 18:37