Solved

How to assign the match of string to another variable in perl?

Posted on 2012-12-21
6
439 Views
Last Modified: 2012-12-21
I try to assign the matched result to another variable but instead the other variable gets the value 1 which means that there is match.

Here is the example:

my ($clientRoot) = $infoArray[3] =~ /Client root:\s.*/;

Open in new window



$infoArray[3]  is something like this: Client root: some/path/in/here

Expected result for $clientRoot:

some/path/in/here

Open in new window

0
Comment
Question by:Tolgar
6 Comments
 
LVL 4

Accepted Solution

by:
brendonfeeley earned 200 total points
Comment Utility
This should work:

my $clientRoot= $1 if $infoArray[3] =~ /Client root:\s(.*)/;
0
 

Author Comment

by:Tolgar
Comment Utility
I normally use my other pattern but why didn't it work in here? Is it because I have an array instead of a single variable?
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 200 total points
Comment Utility
Or, more verbosely:
if ($infoArray[3] =~ m{Client root:\s+(.*)}) {
    $clientRoot = $1;
}

Open in new window

Or, more compactly:
my ($clientRoot) = ($infoArray[3] =~ m{Client root:\s+(.*)});

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Expert Comment

by:brendonfeeley
Comment Utility
You can do it this way:

my ($clientRoot) = $infoArray[3] =~ /Client root:\s(.*)/;

Only difference is the capturing group in the regex.
0
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
Your original pattern did not work because you forgot the capturing parens (see my second option for the correct equivalent of your original idea (which I missed when reading the question)).  The parens around the match may not be required in mine but I usually include them just to make precedence explicit.
my ($clientRoot) = ($infoArray[3] =~ m{Client root:\s+(.*)});

Open in new window

0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
Comment Utility
It did work, but the only thing output is the entire match. This is because you didn't specify any capture groups (parentheses, like brendonfeeley demonstrated). When you capture the result of a regex in list context in Perl, the capture groups are what are output to the variable. Since you didn't specify any groups, the output reverted to the overall match.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now