473,503 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help writting simple regex

6 New Member
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
11 1726
numberwhun
3,509 Recognized Expert Moderator Specialist
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
MrVon
6 New Member
It does not work for me, could you show me how full code will look like?
Dec 1 '09 #3
MrVon
6 New Member
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
3,509 Recognized Expert Moderator Specialist
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
MrVon
6 New Member
It looks like web page HTML with phone on it saying "Phone: (123) 456-7890" somewhere on the page.
Dec 1 '09 #6
numberwhun
3,509 Recognized Expert Moderator Specialist
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
MrVon
6 New Member
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
chaarmann
785 Recognized Expert Contributor
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
MrVon
6 New Member
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
3,509 Recognized Expert Moderator Specialist
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
chaarmann
785 Recognized Expert Contributor
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

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

Similar topics

8
2963
by: Johnny | last post by:
I need to determine whether a text box contains a value that does not convert to a decimal. If the value does not convert to a decimal, I want to throw a MessageBox to have the user correct the...
2
1650
by: JKJ | last post by:
I need help with a regular expression that will pull the title and all the meta tags held in the head section of an HTML file (including the head tags). I want to exclude everything else such as...
6
4775
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
6
6529
by: Raed Sawalha | last post by:
Hello: I have a textbox which let user key in time.. sample of data: 06:00 AM 9:15 PM User only can key in the 12 hour format. how to write the Regular Expression for such input? Thanks!!!!
2
1151
by: Brian Henry | last post by:
Hi everyone, I never have worked much with regex (trying to learn it now myself) but I have a simple problem that I need to solve.. Say I have numbers like this 00023432-234...
2
2357
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the...
2
1613
by: garyusenet | last post by:
Hi All, I have been working on the following programme over the last day or so and have made a good deal of progress. It is a very simple programme, but is proving very useful as a learning aid,...
5
1290
by: Nemisis | last post by:
Hi everyone, I am currently building my .Net website, everything was fine, until i decided to put some generic functions into a module. The module is located in App_Code, with 2 other of my...
5
426
by: Petra Meier | last post by:
Hello, I use the following script to parse URI and email: function parseLinks($sData){ $regexEmail = "/\w+((-\w+)|(\.\w+))*\@+((\.|-)+)*\.+/"; $sData = preg_replace($regexEmail, "<a...
5
1638
by: Maqsood Ahmed | last post by:
Hello, I am trying to create a Regex object which can match ASCII character 0x05 in a given string. I have written following code to accomplish this: System.Text.RegularExpressions.Regex...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.