New topic: RegEx question
<http://forums.realsoftware.com/viewtopic.php?t=47420> Page 1 of 1 [ 3 posts ] Previous topic | Next topic Author Message paulfiner Post subject: RegEx questionPosted: Wed Mar 27, 2013 12:09 pm Joined: Tue Oct 04, 2005 2:33 pm Posts: 94 Location: London, UK I have the following source string "(wave1&(21/1-5))+(w2&(22/1-5))+(wave3&(23/1-5))" What I want to do is extract all the bits of text like 'wave1', 'w2' and 'wave3' which, using an external Regex program, I can accomplish with the following Regex '[a-z]+[0-9]*' In my code I have the following, where s contains the source string. dim rxOptions as RegExOptions = rx.Options rxOptions.Greedy = true rxOptions.LineEndType = 4 dim match as RegExMatch = rx.Search( s ) dim varsFound as Integer if match <> nil then varsFound = match.SubExpressionCount end if When stepping through this in the debugger, varsFound only equals 1. Shouldn't match.SubExpressionCount give the number of times the match was found? Cheers Paul Top ktekinay Post subject: Re: RegEx questionPosted: Wed Mar 27, 2013 12:34 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 536 Location: New York, NY No, that reports the number of subexpressions within a single match, not the number of times a pattern can find a match within a string. For example, if your pattern were instead: ([a-z]+)([0-9]*) SubExpressionCount would be 3. If the matched string were "wave9", the values of SubExpressionString would be: SubExpressionString( 0 ) = wave9 SubExpressionString( 1 ) = wave SubExpressionString( 2 ) = 9 To find the next match, run the search again: dim rxOptions as RegExOptions = rx.Options rxOptions.Greedy = true rxOptions.LineEndType = 4 dim match as RegExMatch = rx.Search( s ) dim varsFound as Integer while match <> nil varsFound = varsFound + 1 match = rx.Search wend _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top paulfiner Post subject: Re: RegEx questionPosted: Wed Mar 27, 2013 1:19 pm Joined: Tue Oct 04, 2005 2:33 pm Posts: 94 Location: London, UK Ahh, thanks for the explanation Kem. I guess your RegExRX program, which I was using, does that in the background when showing the search results. I just assumed that's what SubExpressionCount would return. Cheers Paul Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 3 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
