Jump to content

Stuck on what appears to be a simple regex..


Anti-Moronic

Recommended Posts

I've have been trying for too long now to understand the regex required for this. I'd appreciate any help I can get.

 

This should match:

 

012-345 69

 

However, these should not:

 

-012345 678

01203- 34566

1234x567

 

This is validation is:

 

1) must not begin or end with dash (-), I have trimmed to ensure does not begin or end with space

2) each number may contain a dash or space in between - however, cannot contain any combination of the two in sequence. Like dash dash, dash space, space space, space dash

3) the only other characters which can be present in the number are dash and space.

 

Any ideas?

Link to comment
Share on other sites

You can use trim to remove the dashes from either side too

 

$new_subject = trim($subject, ' -');

 

To match that with regex then use

if(preg_match('~^\d{3}-\d{3} \d{2}$~', $new_subject)) {
    // matched successfully code here
} else {
    // Failed to match code here
}

 

This of course assumes you want to have the dash and space between the numbers exactly as you have it

Link to comment
Share on other sites

Thanks! Sadly, according to 2 your assumption would be incorrect.

 

That is just an example of a match. A few more matches:

 

01-2-345 69

012 345 69

012345-69

 

This is the trouble I had. I am having to match based on each number and the adjacent character. Also, I cannot trim, this must run without trimming dashes.

 

Any other ideas?

Link to comment
Share on other sites

Thanks Jay, I really appreciate your input but those are just examples. I could send over a 100 examples - the main thing I am trying to validate is point 2) above. For example, this should also match:

 

012-345-69

 

even:

 

0-1-2-3-4-5-6-9

 

or

 

0 1 2 3-4-5-6-9

 

String cannot begin or start with dash. after each number there can only be a dash, space or another number, and after each dash or space there cannot be another dash or space.

 

Thanks again. I've validated based on specific examples, but as soon as I try to write one which matches all I get stuck.

Link to comment
Share on other sites

~^(?!-)(\d[\s-]?){1,}(?<!-)$~

 

An alternative approach would be something like the following, which might be easier to understand at-a-glance.

 

/^\d([ -]?\d)*$/D

 

Also note that the literal space character was used (since \s matches more than just the space character) as was the D modifier (which prevents values with trailing newlines from being false positives).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.