Jump to content

Recommended Posts

I am searching for a number pattern in a string, e.g. 123-12 or 123-1234. 123-13-123, 123-123-1234, 123-12x are not a matches.

 

This pattern, \d+-\d+(?=[\s$]) , works except when the number is the only thing in the string, i.e. '123-12' with no spaces following. Add a space on the end, and RegEx works. This was tested in Delphi XE.

 

What am I missing?

 

 

Share this post


Link to post

Outside a set $ matches a position not a character and within a set it matches the character $. One option is to use a capture group with |.  So this pattern:   \d+-\d+(\s|$)

 

Edited by Brian Evans
  • Thanks 1

Share this post


Link to post

Ah. I was trying to say a space or end of line was allowed. Your capture group works better. Thank you. I made small modification for punctuation because "122-22." or "122-22," or the like is acceptable.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×