473,657 Members | 2,587 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 1736
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

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

Similar topics

8
2977
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 value in the text box. I have the following code but when the user enters a decimal value the Regex.IsMatch catches it (ex. 250.50 should be allowed, but 250.50.0 should not). My code is as follows: if( ! Regex.IsMatch( tboxQtyCounted.Text,...
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 link tags, script tags, etc. I have a pretty big process that pulls this stuff now using simple Regex expressions, but I know I'm not using the Regex's to their fullest. . .
6
4789
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 string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
6
6536
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
1160
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 0000000034112-23 00001200034-34
2
2362
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 following: Search a whole blob of text (including newlines and everything). Find any text enclosed in brackets (), and replace it with a string I provide and then concatenate the text that had been enclosed in the brakets, without the brakets.
2
1619
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, and will eventually be useful to me in it's own right. It function is to open a text file, and remove HTTP addresses from the file. The file is always in a certain format, and the HTTP address is always proceeded by a key phrase.
5
1297
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 Modules. but for some reason everytime i try to call the function i get an error of "Name 'GeneralMethods' is not defined". here is my module, as you can see very simple
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 id='external' href='mailto:'$0'>$0</a>", $sData); $regexURI = '#(^|{1})(http://|ftp://|https://|news:)(+) (|$)#sm';
5
1647
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 regex = new System.Text.RegularExpressions.Regex("*?", System.Text.RegularExpressions.RegexOptions.Compiled);
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6164
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
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 we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.