Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex Findrx Problem
#1
Hi,
I am trying to do something which I think should be relatively simple in RegEx (I know, I know...nothing is simple in RegEx ...but it's like magic when it does work!)
Anyway here is an abstract example of the problem: how to get only the first submatch

Macro
Code:
Copy      Help
str sample =  "There was this dog who met another dog in the dog park then ran to another dog park and then left to meet another dog"
str result
findrx(sample "((?<=dog).+(?=dog))" 0 0 result)
out result

The result is
Quote:who met another dog in the dog park then ran to another dog park and then left to meet another

when I want it to be only the first instance ie.
Quote:who met another

I tried making "dog" to "dog" a submatch and then requesting the first submatch (from help file: A captured subpattern is the part of pattern that is enclosed in parentheses and does not begin with ?.) , but it didn't work

Macro
Code:
Copy      Help
str sample =  "There was this dog who met another dog in the dog park then ran to another dog park and then left to meet another dog"
str result
findrx(sample "(dog.+dog)" 0 0 result 1)
out result

or

Macro
Code:
Copy      Help
str sample =  "There was this dog who met another dog in the dog park then ran to another dog park and then left to meet another dog"
str result
findrx(sample "((?<=dog).+(?=dog))" 0 0 result 1)
out result

but neither worked i.e. got the whole thing again.

I also tried making result an ARRAY(str) but with less luck.

Thanks so much for any insight.

Stuart
#2
? makes .+ ungreedy.

"((?<=dog).+?(?=dog))"
#3
Worked Perfectly. Never understood the concept of greedy/ungreedy before (with regard to RegEx not in life!!!!!).
Thanks again Gintaras,
Stuart


Forum Jump:


Users browsing this thread: 1 Guest(s)