Hello

If we have code like this

$content =~ m/test/;

than it will return true or false, but how can I put a match of a regex into a scalar instead of it returning true or false?

Thank You

Use capturing parentheses, this is covered in any basic regexp tutorial. One example:

$content = 'this is a test';
$content =~ m/(test)/;
$match = $1;
print $match;

Of course that is silly because you already know you are looking for "test" so there is no need to capture it and assign it to a scalar. All you need to know is if the regexp is true then you can assign "test" to a scalar. But maybe you are really looking for a pattern of unknown value you want to capture, something like:

$var = 'test 3456';
if ($var =~ /(\d+)/) {
   print $1;
}

which will print 3456

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.