Reputation: 7592
Example text:
This is my car, I don't like it, because it is slow. This is my car, SomeRandom24324<>, it is slow. This is my car, how about you and me..., because it is slow.
I want to remove everything in between "This is" and "is slow".
I tried (?<=This is)(.*?)(?=is slow)
, but it only removes first occurrence and I get:
This isis slow. This is my car, SomeRandom24324<>, it is slow. This is my car, how about you and me..., because it is slow.
Rest of occurrences are still there.
As you understand I want to have only:
This isis slow. This isis slow. This isis slow.
PS: Any good book to learn regex?
Upvotes: 0
Views: 2597
Reputation:
Doing this can depend on the language you are using. In perl I would use the substitute function which uses regexes, eg. $line=~ s/regex_with_2_groups/"$1 whatever-u-want $2"/g, but in java I might use String.replaceFirst() or String.replaceAll and in python re.sub(). An approach is to match '(2save1)toRemove(2save2)' and output "group1 whatever-u-want group2".
Great references are Mastering Regular Expressions and Regular Expressions Cookbook. See http://www.regular-expressions.info/ for regex deatails of all major languages that have them and software for testing and analyzing regexes and http://www.regexr.com/ has a free online tool for learning, building and testing regexes.
Upvotes: 0
Reputation: 70750
Your regular expression is fine. I would try enabling the dotall modifier to force the dot across newlines as well.
String result = Regex.Replace(input, @"(?s)(?<=This is).*?(?=is slow)", "");
As far as learning regular expressions, I would begin here: Regular-Expressions.info — RexEgg
Upvotes: 2
Reputation: 8774
The regex is fine. You need to tell whatever you put that regex into (some replace
call, probably) to replace all matches, not just one. That is not part of the regex, but a flag to the replacement instruction/call/whatever.
Upvotes: 1