Jump to content

Recommended Posts

Im trying to find a regular expression that can take 10 digit numbers from a code this is formatted like this

 

n the Blarney Bypass     Today Ipm   
Road     
Women's     c\. Superb Range of Fine   
  Clothes for Men & Women   
Christmas     Reduced   
Special     Opening Hours:   
  Tue - Sat lOam - 6pm   
Afternoon / Evening     Sunday 2.l5pm - 6pm   
Tea     ( Opposite Killarney Golf &   
Booking Essential     Fishing Club   
Tel: 087-2359046     Fossa, Killarney, Co. Kerry   
  Tel: 064-6632659   
Godsils       
Castle Street, Cork       
2010     Kamara   
COMMUNION     Interiors   
DRESSES     
Fabulous Selection of     East Douglas Village   
Communion Dresses     
&     Cork   
Accessories       
Now In Stock     New Year Sale   
Sizes 24-40     Now on!   
No Appointment Necessary     
Personal Attention     All stock reduced   
Guaranteed     
Ph: (021) 4273863     Tel 021-4898581   

-, , /0 LJ B L I NTH EAT REG U IDE ' SAT JAN 2 

,l' !elSEY TH E A'TRE THE SEAFARER Cast: Liam Carney, Phelim Drew, Nick Dunning, Maeliosa Stafford, Don 

~ M S ~-~~-~~~~. 

 

I only want to extract 10 digit numbers that start with 087xxxxxxx, 086xxxxxxx, 085xxxxxxx or 083xxxxxxx

 

and the numbers can be in this format

 

087xxxxxxx

087 xxxxxxx

087-xxxxxxx

(087)xxxxxxx

(087) xxxxxxx

 

This expression will match all format and numbers that start with any number.

 

'/(\d{10})|(\d{3}-\d{7})|(\d{3}\'\d{7})|(\(\d{3}\)\s+\d{7})|(\(\d{3}\)-\d{7})|(\(\d{3}\)\d{7})|(\d{3}\s+\d{7})|([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})/sim'

 

What does ?: mean in this code (?:[A-Z0-9-]+\.)

does the +\. mean plus any digit?

 

What does /sim mean? i never came across this before?

 

Can some one write a regular expression to only match the numbers in the format i have specified and that start with 086, 087, 085 or 083

 

Thanks

Something like this should do...

 

"#08[3567][) -]{0,2}[0-9]{7}#"

 

It matches 08 followed by either 3, 5, 6 or 7. Followed by 0 - 2 characters from either space closing bracket or dash, followed by 7 numbers. You may well want to add some capture groups and the optional opening bracket at the start, it all depends exactly what format you want the captured string to be in.

 

Edit: The ?: makes the brackets a non-capture group. The + means one or more of the characters in the character group before it and the \. means match a literal fullstop. The / at the end is a closing delimiter and the sim characters are all modifiers. s makes any non-escaped fullstop in the pattern match newline characters, i makes the pattern case insensitive and m I believe turns on multi-line mode. You can find out most of this information from either the PCRE section of the PHP manual or via http://www.regular-expressions.info.

THats cool :) Thanks

 

If i want to ad in the optional ( character at the start how do i do this. I tryed this but it wouldn't work

 

"#[(?]08[3567][) -]{0,2}[0-9]{7}#"

 

Im looking for numbers in this format as well

 

( 087 ) xxxxxxx

(087) xxxxxxx

(087)xxxxxxx

 

And also if you can help.. How would i strip out all the extra characters and make them into just 10 digit characters without any brackets - etc..

 

at the moment i have this code

 

preg_match_all("#[(?]08[3567][) -]{0,2}[0-9]{7}#",$file,$matches);
	foreach($matches[0] as $key=>$val) {echo $val . "<br>";}                                                                                                                  
	exit;

 

Hope you can help and thanks

 

To put in the optional ( caracter but it only returned one number...

Since you want a 10 digit unformatted number, there is really no point matching the opening bracket because your going to be ignoring it. The pattern I provided will match the last two formats you listed it just doesn't capture the opening bracket, which you just said you didn't want. To make the first one match just change the 2 in {0,2} to a 3, that way it will match up to three of the characters which means it would match " ) ". I can't think off the top of my head of a way of capturing only the numbers, which means you will need to make two capture groups and then stitch them together afterwards, something like...

 

"#(08[3567])[) -]{0,2}([0-9]{7})#"

 

Then use $out[1][x] . $out[2][x] to get the full number.

 

Incidentally the reason your pattern didn't work is because you placed the bracket and question mark inside a character class, meaning that your pattern would match either an opening bracket or a question mark, and one of those characters is required. Removing the square brackets from around it would make it an optional opening bracket.

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.