• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Perl string parsing

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
robocop100
Asked:
robocop100
  • 2
1 Solution
 
kivan24Commented:
$subject = "200 result=12345";
if ($subject =~ m/=(\d{1,5})/m) {
	$result = $&;
} else {
	$result = "";
}
print $result;

Open in new window

0
 
robocop100Author Commented:
the string result may be of unknown length (it's just 5 in this example)
0
 
kivan24Commented:
$subject = "200 result=12345234769396";
if ($subject =~ m/=(\d+)/m) {
    $result = $1;
} else {
    $result = "";
}
print $result;

Open in new window

0
 
wilcoxonCommented:
Even simpler...

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now