as opposed to regex:'foo.+bar'
views:
68answers:
4
+5
A:
Use a group:
foo(.+?)bar
Then you will be able to refer to the group as $1
or \1
, depending on the language and what you are doing with it.
As always, let me recommend Regular-Expressions.info for learning all about regexes.
Michael Myers
2010-02-23 17:37:33
thank you! -marienbad
marienbad
2010-02-23 17:40:18
Not sure if not hungry notation (.*?) is available in all regex implementations.
lollinus
2010-02-23 17:41:13
A:
You can use:
foo(.+?)bar
or
foo(.*?)bar
The 2nd one will work even when there is nothing between foo and bar
codaddict
2010-02-23 17:40:21
+2
A:
An alternative would be to use lookaround if the regex flavor you're using (which you didn't specify) supports it. .NET, Python do, Ruby and JavaScript don't (fully), for example:
(?<=foo).+(?=bar)
matches any number of characters if they are preceded by foo
and followed by bar
.
Tim Pietzcker
2010-02-23 17:40:37