The online racing simulator
[OT] Phone Numbers.
1
(29 posts, started )
[OT] Regular Expression
(123)-456-7890

I want to make sure that they input their number correctly, I think this is the best way to do that. It requires that they input their area code, and thus I get the 9 digit phone number that I need for the SMS system to work.

Victor, you might also want to look at this topic.
I looked at the SMS system within LFSWorld and was very much confused by the +1 (0).
I understand the +1 (That's the USA), but the (0), I don't quite understand where that comes in, however that's on an international level, and one that I don't have to be worried about in this case.

So, what Regular Expression would I use to ensure that they input the correct format?
Assuming the rest of the world follows UK standards, typically when you input an UK number from an international destination you drop the leading 0 in favour of the country code.

i.e. My number maybe 01234 123456 (it's not incidentally), and the UK country code is +44, so the international number for that would be +441234123456, which over here can also be written +44 (0) 1234 123456.

Hopefully that answers your question? If there was one?
Nice, thanks TAA for the education .
Now, if only I could get some help with the PREG stuff.

This was my shot, but apparently I suck at this:

<?php 
php
    preg_match
('/(^[0-9]/)-^[0-9]-^[0-9]/''(123)-456-7890'$return);
    
print_r($return);
?>

Quote :Warning: preg_match() [function.preg-match]: Unknown modifier ')' in /test/www/phone.php on line 2

Are you planning on going all out, or do you just want to support the (123)-456-7890 format?

Edit: The following regex would match your example, but isn't particularly flexible.
\(\d{1,3}\)-\d{1,3}-\d{1,4}

Quote from the_angry_angel :Are you planning on going all out, or do you just want to support the (123)-456-7890 format?

Edit: The following regex would match your example, but isn't particularly flexible.
\(\d{1,3}\)-\d{1,3}-\d{1,4}


Funny, that did not work for me. I just replace the first arg in the preg_match function with your code, encased in signal quotes, and I get this error;
Quote :Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\wlvac\phone.php on line 2

You'll need to wrap that in /'s
Quote from the_angry_angel :You'll need to wrap that in /'s

Ok, could you explain why?

And thank you, it worked. Could you point me to a good resource on this topic. I find the PHP Manual to be very much useless on this topic.
Quote from Dygear :Ok, could you explain why?

If you use perl compatible regex you can add additional options (aka modifiers) after the regex, such as making it case insensitive, or greedy, etc. Therefore you need some way to describe what's the regex and what any additional options are. PHP's functions assume that the first character should be considered to be the delimiter. As such you should avoid using anything that is a known regex operator or symbol. This includes \ which is used to escape a character from being evaluated.

Usually you'll see people use / as the delimiter, as that's what is used in perl.

Quote from Dygear :And thank you, it worked. Could you point me to a good resource on this topic. I find the PHP Manual to be very much useless on this topic.

Regex's are very much a case of practising and sometimes trial and error If you're going to use perl compatible (which I suggest, as they are by and large "the standard").

Admitedly the PHP manual is crap, however they've got a good summary of modifiers and a basic reference on writing a pattern which is awful, but useful if you need to quickly check something whilst you're learning or haven't used a regex for a while.

regular-expressions.info is a site I've pushed a few people to over recent months, but I'm not convinced how useful it is any more.

The main issue with regex's is that they're so flexible they're quite tricky to explain without a good set of practicals.

My favourite tool for writing and testing a regex is RegEx Coach, which is absolutely awesome if you're having trouble or want to test a quick regex.
Looks like I am getting ... OK ... at this. Still will would accept a value such as 22/3924, and that would not make sense.


<?php 
preg_match
('/\d{1,2}\/\d{1,4}/''10/2010'$return);
print_r($return);
?>

Need to update this, and I will, first (working) attempt, so not bad .
You need the delimiters within the preg_match() call, else it assumes the first char to be so:

/\(\d{1,3}\)-\d{1,3}-\d{1,4}/

Although as you may well know, in PHP, the delimiters don't _have_ to be a '/' char, they can be pretty much any symbol. If I have a few backslashes in a regex string for example, rather than needing to escape them all the time, I'll simply use something like '@' for the delimiters:

preg_match('@\(\d{1,3}\)-\d{1,3}-\d{1,4}@', $foo);

etc etc etc

You might also find start / end anchor points useful for strictness in matching:

preg_match('/^\(\d{1,3}\)-\d{1,3}-\d{1,4}$/', $foo);

This will prevent something like 'foobarbaz(0123)-456-7890bazbarfoo' being a valid entry.

Something you might find useful, I've used it for a few years now, has helped a lot with more complex regex: The Regex Coach.



Regards,

Ian
Quote from Dygear :Looks like I am getting ... OK ... at this. Still will would accept a value such as 22/3924, and that would not make sense.

That's because you've told it to

preg_match('/\d{1,2}[B]\/[/B]\d{1,4}/', '10/2010', $return);

Edit
* the_angry_angel highfives Ian.H
That RegEx Couch is a brilliant program!
Heh.. didn't see your reply TAA (I should refresh the page before responding I guess)

In general, if your looking for reading material, looking for references within the Perl "community" would probably yield more results than limiting things to PHP. 99% of Perl info will translate straight to a preg_match() call with little effort



Regards,

Ian
Quote from Ian.H :Heh.. didn't see your reply TAA (I should refresh the page before responding I guess)

I think we were both typing at the same time some how
EMT Certification Number: \d{6}
EMT Expiration Date: \d{2}\/\d{4}
Cell Phone Number: \(\d{3}\)-\d{3}-\d{4}

I need to do some what better on the Expiration Date, so that it only accepts the values from 01 to 12 before the forward slash and only accepts the rage 1900-2099 for year.
Untested:

^(?:0[1-9]{1}|1[0-2]{1})\/(?:19[0-9]{2}|20[0-9]{2})$

Regards,

Ian
Quote from FireTracker :**WLVAC SIG 9** [UTA - Unknown Type Aided] 100 ADDRESS AVE C/S: STREET LA / ROAD PL - M-10 IN: CITY TOA: 07:30 [FireTracker v2.7]

From this I am trying to capture WLVAC SIG 9, as it also could be WLVAC MUTUAL AID, or something else. So I am trying to get the text between the '**'s.

The next thing I am trying to capture is the call type that is found between the '[' & ']'. Now the call type is not always present, so that might be an issue.

I'd also like to get the Time of Alarm or 'TOA', and the City it is in.


<?php 
array(
    
'larm' => 'WLVAC SIG 9',
    
'type' => 'UTA - Unknown Type Aided',
    
'addr' => '100 ADDRESS AVE',
    
'cros' => 'STREET LA / ROAD PL',
    
'mapl' => 'M-10',
    
'city' => 'CITY',
    
'time' => '07:30'
)
?>

This is above my head, so I bring it to the wisdom of the community. Or TAA & Ian in this case.
This _should_ work for you (quickly constructed / tested in Regex Coach only):


^\*{2}([a-zA-Z0-9\s]+)\*{2} \[*(.*?)\]*.*? IN: ([a-zA-Z0-9]+) TOA: ([0-9]{2}:[0-9]{2})

As a breakdown:
  • Find 2 * chars at the beginning.
  • Grab any a-Z0-9 and space chars
  • Stop grabbing chars when finding 2 more * chars.
  • If '[...]' exists, grab anything in between the [] chars, if not, ignore it.
  • Locate 'IN: ' and grab any chars after it up to ' TOA:'
  • Locate 'TOA: ' and grab 2 numerical chars followed by a : followed by 2 numerical chars.
I removed both the content between the [] chars and removed the actual [] chars as well as you said it might not be present (but didn't know whether the [] chars would still be there, albeit with nothing in between) from my tests and all 3 variations passed as expected.

HTH



Regards,

Ian
With the [], more often then not they are they, and they contain the call type, if you could capture them when they are there that would be great! Works well 'as is', very grateful, thanks for the help!

More examples would be.

Quote from FireTracker :**WLVAC SIG 9** [MALE FELL FX NOSE] SOUTH CITY LANES 25 ADDRESS AVE C/S: STREET LA / ROAD PL - M-10 IN: CITY TOA: 07:30 [FireTracker v2.7]

Quote from FireTracker :**WLVAC SIG 9** [UTA] 25 ADDRESS AVE C/S: STREET LA / ROAD PL - M-10 IN: CITY TOA: 07:30 [FireTracker v2.7]

Quote from FireTracker :**WLVAC SIG 9** 75 ADDRESS ROAD C/S: STREET ROAD / LANE LA - G-7 IN: CITY TOA: 02:43 [FireTracker v2.7]

Quote from FireTracker :**WLVAC MUTUAL AID** [1 AMBULANCE TO STAGING AREA] ADDRESS ROAD C/S: STREET LA IN: TOWN 29 TOA: 22:03 [FireTracker v2.7]

Quote from FireTracker :**WLVAC MUTUAL AID** [SIG 38] SPORTING GOODS 2000 MAIN TPK C/S: EAST WEST AVE / ADDRESS ST - A TONES IN: AGENCY 29 TOA: 08:42 [FireTracker v2.7]

^\*{2}([a-zA-Z0-9\s]+)\*{2} (\[*.*\])*.*? IN: ([a-zA-Z0-9]+) TOA: ([0-9]{2}:[0-9]{2})

This should work. Now they're within '$2' and the * char following will allow for them to be present and absent.

Tested with '[UTA........]', '[]' and missing entirely, seems to work ok



Regards,

Ian


EDIT: I need to redo something as it fails with your other examples. WIll sort it though
Adding more examples to the ones provided above.

Quote from FireTracker :**WLVAC MVA** 10 ROAD LA C/S: STREET LA / ADDRESS ROAD - F TONES / FD 620 IN: TOWN TOA: 07:37 [FireTracker v2.7]

Quote from Ian.H :EDIT: I need to redo something as it fails with your other examples. WIll sort it though

That might be so, but this is insanely good work!
This one works with all examples:

^\*{2}([a-zA-Z0-9\s]+)\*{2} (\[*.*\])*.*? IN: ([a-zA-Z0-9\s-.]+) TOA: ([0-9]{2}:[0-9]{2})

I was missing the option of the city containing a space so have added that and also allowed for '-' and '.' chars within the city match as I guess both could be a possibility? If never going to happen, you can remove the '-.' from the '([a-zA-Z0-9\s-.]+)' match



Regards,

Ian
Beware of the "-" in character classes, they have special meaning.

To allow a "-" in a character class, use it in first or last position of a character class.

So change this:
^\*{2}([a-zA-Z0-9\s]+)\*{2} (\[*.*\])*.*? IN: ([a-zA-Z0-9\s-.]+) TOA: ([0-9]{2}:[0-9]{2})

to this: (swaping "-" and ".")
^\*{2}([a-zA-Z0-9\s]+)\*{2} (\[*.*\])*.*? IN: ([a-zA-Z0-9\s.-]+) TOA: ([0-9]{2}:[0-9]{2})


Just nitpicking
1

[OT] Phone Numbers.
(29 posts, started )
FGED GREDG RDFGDR GSFDG