PDA

View Full Version : Regular Expression named capture group problem


Gravey
09-10-2009, 07:41 AM
the string i'm trying to match is: "c01_c01_c01dirt"

the regex I am using that works is "([crs][0-9]{2}(dirt)?)_([crs][0-9]{2}(dirt)?)_([crs][0-9]{2}(dirt)?)"
to simplify a bit, I use:
pat = "([crs][0-9]{2}(dirt)?)"
pattern = pat + "_" + pat + "_" + pat

I am trying to use a named capture group to achieve the same result. This is what I came up with:
"(?<a>([crs][0-9]{2}(dirt)?))(_\k<a>){2}"

but for some reason it no longer matches the full string because the optional (dirt)? part seems to be ignored. Any ideas?

biddle
09-12-2009, 12:29 AM
Maybe something like this?

"([crs][0-9]{2})(_\1){2}dirt"


--test
pattern = @"([crs][0-9]{2})(_\1){2}dirt"
regex = dotnetobject "system.text.regularexpressions.regex" pattern
m = regex.match "c01_c01_c01dirt"
format "Matched text is [%]" (m.tostring())
format "regex.success == %\n" m.success


EDIT:

Oops, didn't see that "dirt" suffix was optional

Try...
"([crs][0-9]{2})(_\1){2}(dirt)?$"


pattern = @"([crs][0-9]{2})(_\1){2}(dirt)?$"
regex = dotnetobject "system.text.regularexpressions.regex" pattern
m = regex.match "c01_c01_c01dirt"
format "Matched text is [%]" (m.tostring())
format "regex.success == %\n" m.success
m = regex.match "c01_c01_c01"
format "Matched text is [%]" (m.tostring())
format "regex.success == %\n" m.success
m = regex.match "c01_c01_c01blah"
format "Matched text is [%]" (m.tostring())
format "regex.success == %\n" m.success
m = regex.match "c01_c01_c01dirty"
format "Matched text is [%]" (m.tostring())
format "regex.success == %\n" m.success

Gravey
09-14-2009, 03:57 AM
Cheers Mike, works great.

Gravey
09-14-2009, 04:06 AM
actually almost as soon as I posted I found out that it doesnt work how I need it to. the dirt part is optional after every c01. so all these would match:
c01_c01_c01
c01dirt_c01_c01
c01_c01_c01dirt
c01dirt_c01dirt_c01dirt
etc...

biddle
09-14-2009, 04:46 PM
Try this one:

([crs][0-9]{2})(dirt)?(_\1(dirt)?){2}$


pattern = @"([crs][0-9]{2})(dirt)?(_\1(dirt)?){2}$"
regex = dotnetobject "system.text.regularexpressions.regex" pattern

tests = #(
"c01_c01_c01dirt",
"c01_c01dirt_c01",
"c01dirt_c01_c01dirt",
"s01_s01_s01",
"c01_c01_c01blah",
"c01dir_c01_c01dirt"
)

for t in tests do (
format "\nChecking [%]\n" t
m = regex.match t
if m.success then
(
format "Matched text is [%]\n" (m.tostring())
)
else
(
format "No match!\n"
)
)

CGTalk Moderation
09-14-2009, 04:46 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.