Regular Expression named capture group problem

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 09 September 2009   #1
Regular Expression named capture group problem

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?
 
Old 09 September 2009   #2
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   
   

Last edited by biddle : 09 September 2009 at 12:39 AM.
 
Old 09 September 2009   #3
Cheers Mike, works great.
 
Old 09 September 2009   #4
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...
 
Old 09 September 2009   #5
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"
  )
)
 
Old 09 September 2009   #6
Thread automatically closed

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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 03:03 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.