By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

Help writting simple regex

P: 6
Hi, i trying to write regular expression that looking for phone number in text file, but only does it if there is "Phone: " in front of number on same line.

Expression i created looks like this
Expand|Select|Wrap|Line Numbers
  1. (?:(?:\d{1,3}[-. ]?)?[(]?\d{2,4}(?:[-). ]|[)][ ]))?\d{1,4}[-. ]?\d{4}(?:[-/.* ]\d{1,5})?
How i add "Phone: " check to it?
Nov 29 '09 #1
Share this Question
Share on Google+
11 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
You can add any text in your regex that you expect to see.

For instance, if in your case the "Phone:" is at the beginning of the line in question, you could do the following:

Expand|Select|Wrap|Line Numbers
  1. m/^Phone:/
  2.  
That will match lines that start with "Phone:". You can simply add your regex after it.

Hope that helps.

Regards,

Jeff
Nov 29 '09 #2

P: 6
It does not work for me, could you show me how full code will look like?
Dec 1 '09 #3

P: 6
The thing is that i actually use program that supports perl 5 programming language regular expressions, so could you please help me just add to my line of code check for "Phone: " in beginning of number.
Dec 1 '09 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
I wish I could, but I have no idea what the data you are dealing with looks like. You would need to post a sample.

Did you look at the code I posted above? That will find lines that start with "Phone:". If you want to match anything after it, you just add to it.

As for the version of Perl, I have only coded in Perl 5 and have not started on Perl 6, so this should work fine for you.

Regards,

Jeff
Dec 1 '09 #5

P: 6
It looks like web page HTML with phone on it saying "Phone: (123) 456-7890" somewhere on the page.
Dec 1 '09 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
In your original thread start message, you said it was a text file, not an HTML file.

If its an HTML file, its a different game and you will have to parse the HTML and get the value from the element that it is in.

Otherwise, if it is in a text file, and in the format that you described, try something like the following:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $string = "Phone: (123) 456-7890";
  7.  
  8. if( $string =~ /^Phone:\s+(\(\d{3}\)\s*\d{3}-\d{4})/){
  9.     print("The phone number is: $1\n");
  10. }
  11. else{
  12.     print("Sorry, no number found.  Adjust your regex.\n");
  13. }
  14.  
Again, if its inside of html, you will have to look at modules to parse html.

Regards,

Jeff
Dec 1 '09 #7

P: 6
I dont have perl experience, program i use allow writting scripts in perl 5, its just string field where you can enter code you want to use, thats what program documentation saying.

Regular Expression: A regular expression is a formal description of a template to be matched against a text string. It is a pattern that is matched against a subject string from left to right, through using a sequence of escape characters to describe a set of characters with special meanings.

A regular expression (sometimes abbreviated to "regex") is a way for a computer user or programmer to express how a computer program should look for a specified pattern in text and then what the program is to do when each pattern match is found. For example, a regular expression could tell a program to search for all text lines that contain the word "Windows XP" and then to print out each line in which a match is found or substitute another text sequence (for example, just "Windows") where any match occurs.

Regular expressions are used for advanced context sensitive searches and text modifications.

The syntax of regular expressions in InfoSeek is Perl 5 compatible. To distinguish from normal expressions, a regular expression defined in InfoSeek must contain a comment: (?#xxx) - xxx is the content of the comment and it may be empty.

We advise common users to use normal expressions only because of the complexity and speciality of regular expressions.
I need someone to edit code i posted in first message (that is actually works) to get not all phone numbers, but only ones with "Phone: " in front of it, can you suggest how this line of code should look like?
Dec 3 '09 #8

Expert 100+
P: 785
Read about "positive lookbehind" in regular expressions.

That means, simply add "(?<=Phone:)" in front of your regex-string.

As what I understand from the listing above is that you are using InfoSeek and have only a single input line for a pearl-like regex-string.
You should have told us that before.
Dec 3 '09 #9

P: 6
Please problem is still unsolved, i need complete string of regular expression to use.

Or can someone write REGEX that searches "Phone:" and then shows 14 characters that come after it as result, only thing it has to be one line.
Dec 17 '09 #10

numberwhun
Expert Mod 2.5K+
P: 3,503
Ok, lets take a step back here.....

In post #6 you gave a sample of what you need to match. In post #7 I gave you some code that matches what you needed to match and captures the number itself. That is easily modified to do whatever you need to do.

I don't know what else you are looking for but considering you have admited in post #8 to having no Perl experience, that tells me that you need to pick up a tutorial and start learning because at this point, I don't think you actually know what you want.

If you know nothing of the language and its capabilities, how are you going to learn or know what you want to do?

I am not trying to be mean, just pointing out that you really need to learn some Perl so you can understand fully what you are being provided, especially since an answer in the form of code was given to you.

Regards,

Jeff
Dec 17 '09 #11

Expert 100+
P: 785
And what about my answer in post #9 ???
I wrote:
"...simply add "(?<=Phone:)" in front of your regex-string."
Have you tried it?

If you have a space after the word "Phone" before the phone number, than you can try with "(?<=Phone: )" instead. That means, your whole regex will be
Expand|Select|Wrap|Line Numbers
  1. (?<=Phone: )(?:(?:\d{1,3}[-. ]?)?[(]?\d{2,4}(?:[-). ]|[)][ ]))?\d{1,4}[-. ]?\d{4}(?:[-/.* ]\d{1,5})?
  2.  
This is a simple copy-and-paste operation that you can do without any understanding of regular expressions or perl.

This is your complete solution. It should work.
If not then please tell me the effect (or error) it caused more detailed.
A "problem is still unresolved" as answer is not enough.
Dec 17 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.