Question

I'm trying to capture a string that is like this:

document.all._NameOfTag_ != null ;

How can I capture the substring:

document.all._NameOfTag_

and the tag name:

_NameOfTag_

My attempt so far:

if($_line_ =~ m/document\.all\.(.*?).*/)
{

}

but it's always greedy and captures _NameOfTag_ != null

Was it helpful?

Solution

The lazy (.*?) will always match nothing, because the following greedy .* will always match everything.

You need to be more specific:

if($_line_ =~ m/document\.all\.(\w+)/)

will only match alphanumeric characters after document.all.

OTHER TIPS

Your problem is the lazy quantifier. A lazy quantifier will always first try and rescind matching to the next component in the regex and will consume the text only if said next component does not match.

But here your next component is .*, and .* matches everything until the end of your input.

Use this instead:

if ($_line_ =~ m/document\.all\.(\w+)/)

And also note that it is NOT required that all the text be matched. A regex needs only to match what it has to match, and nothing else.

Try the following instead, personal I find it much clearer:

document\.all\.([^ ]*)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top