?
Solved

Perl string parsing

Posted on 2011-05-13
4
Medium Priority
?
292 Views
Last Modified: 2012-05-11
I have a string with the value:

200 result=12345

There is sometimes a newline character after the 5, sometimes not.

Using Perl how can I extract 12345 into a new variable?
0
Comment
Question by:robocop100
  • 2
4 Comments
 
LVL 8

Expert Comment

by:kivan24
ID: 35754422
$subject = "200 result=12345";
if ($subject =~ m/=(\d{1,5})/m) {
	$result = $&;
} else {
	$result = "";
}
print $result;

Open in new window

0
 

Author Comment

by:robocop100
ID: 35754430
the string result may be of unknown length (it's just 5 in this example)
0
 
LVL 8

Accepted Solution

by:
kivan24 earned 200 total points
ID: 35754483
$subject = "200 result=12345234769396";
if ($subject =~ m/=(\d+)/m) {
    $result = $1;
} else {
    $result = "";
}
print $result;

Open in new window

0
 
LVL 27

Expert Comment

by:wilcoxon
ID: 35754724
Even simpler...

$string = "200 result=12345\n";  # below works with or without the newline in the string
$string =~ m{=(.*)$};
$result = $1;
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question