RegEx Problem

Hey all,

I’m delving into my first foray into Regex, but my loop below is infinite. I want to pick out the three dates. What am I doing wrong??

  [code]
  dim s as string = "Date one is 20/10/1950, date two is 15/03/2010 and date three is 26/01/1988"
  dim rx as new RegEx
  dim rxm as RegExMatch
  
  rx.SearchPattern= "\\b(\\d{1,2})([/-])(\\d{1,2})([/-])(\\d{4}|\\d{2})\\b" 
  rx.SearchStartPosition = 0
  rxm = rx.Search(s)
        
  while rxm <> nil
      '...do my stuff here
             
      rx.SearchStartPosition = rxm.SubExpressionStartB(0) + rxm.SubExpressionString(0).LenB
      rxm = rx.Search(s)
     
  wend[/code]

I use RegExRx and can see that the search pattern is working, but my code always finds the first date and loops continually.

Any help appreciated.

Cheers
Grant

and if you comment out the line

rx.SearchStartPosition = rxm.SubExpressionStartB(0) + rxm.SubExpressionString(0).LenB

Assuming your “stuff” isn’t changing s, you don’t need to set the next start positions yourself. And when you use that form of Search, it resets it to zero anyway.

while rxm isa RegExMatch
  // do my stuff
  rxm = rx.Search // With no parameter, it will match next
wend

As for your pattern, it would also match something like “11-22/33”. How about this?

rx.SearchPattern = _
  "\\b([0-2]\\d|3[0-1])([-/])([0]\\d|1[0-2])\\g2([12]\\d{3}|\\d{2})\\b"

The values you want would be in groups 1, 3, and 4.

Thanks Kem. Problem solved and working correctly now.

Cheers
Grant