LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2011, 10:49 AM   #1
kcleveland
LQ Newbie
 
Registered: Jun 2010
Posts: 28

Rep: Reputation: 0
Question Perl - regular expression to match variable number of digits after string


I'm trying to parse a number out of the following string using perl (not my forte, as you can see):

Code:
Contact: 5005 <sip:5005@127.0.0.1:5062>
I need to get the "5005" part that comes directly after "Contact:"

5005 will be a different number each time (and can be any length between 4 digits and 12 digits), so I can't rely on matching to a constant.

I've tried several variations of this (For some reason, I'm terrible with regular expressions, and always have been, which is why I must ask for help on this one):

Code:
$SIPuserID=~s/\Contact:[^\n]*\n/s;
I have tried for hours with no success - can anyone help me out here?

Thanks!
 
Old 12-06-2011, 11:02 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
This prints out 5005:

Code:
#!/usr/bin/perl
$SIPuserID = "Contact: 5005 <sip:5005@127.0.0.1:5062>";
$SIPuserID =~ s/^Contact: ([0-9]*) .*/$1/;
print "$SIPuserID\n"
The ^ means "line starts with"

() means "save whatever you find here" (so we can reference it with $1.)
[0-9]* matches a sequence of 0-n digits.
.* matches whatever else may come after.

Regular expressions are extremely useful, I use them constantly. If you're not feeling excellent at them, I would highly recommend setting aside an afternoon to buff up.

Last edited by jhwilliams; 12-06-2011 at 11:08 AM.
 
1 members found this post helpful.
Old 12-06-2011, 11:27 AM   #3
kcleveland
LQ Newbie
 
Registered: Jun 2010
Posts: 28

Original Poster
Rep: Reputation: 0
Thanks jhwilliams!

I will take your advice - this Saturday afternoon has officially become "Regex Saturday".

Thanks for the code, it works perfectly.
 
Old 12-06-2011, 11:30 AM   #4
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Quote:
Originally Posted by kcleveland View Post
Thanks jhwilliams!

I will take your advice - this Saturday afternoon has officially become "Regex Saturday".

Thanks for the code, it works perfectly.
Excellent! I'm having another AWK Saturday, myself.
 
Old 12-08-2011, 04:46 AM   #5
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Note: I have repeated the variable to show to examples:


Source File:
$ cat oye.pl
Code:
#! /usr/bin/perl 
$SIPuserID = "Contact: 5005 <sip:5005@127.0.0.1:5062>";
if($SIPuserID =~ /Contact: ([0-9]+)\s/){ print $1; print "\n";}

$SIPuserID = "Contact: 50058788770 <sip:5005@127.0.0.1:5062>";
if($SIPuserID =~ /Contact: ([0-9]+)\s/){ print $1; print "\n";}
A Sample Run:
$ ./oye.pl
Code:
5005
50058788770
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is there a command that can match a string with regular expression in linux ? 915086731 Linux - General 13 08-22-2011 02:45 AM
Regular expression to match unspecified number of characters until a '>' cygnal Linux - General 7 07-15-2010 12:21 PM
bash: checking if a variable is a number (need regular expression help) anonguy9 Linux - Newbie 6 03-29-2009 02:37 AM
perl regular expression a char match richikiki Programming 8 07-19-2006 03:37 AM
Regular expression to match a valid URL string vharishankar Programming 13 07-21-2005 09:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration