Saturday 15 August 2015

regex - How do I grab only one capture out of a Perl regular expression? -


In addition to following a match, it is an easy way to catch only one element out of a match:

  My $ date = ($ xml_file = ~ m / (\ d +) - sys_char /) [0]; # Or my $ date = $ 1 if $ xml_file = ~ / (\ d +) - sys_char /;  

Is there any flag to specify that the meter does not return an array, but all $ # matches have only one consistent value, so can I?

  My $ date = ($ xml_file = ~ mSOMEOPT / (\ d +) - sys_char /);  

Delete from the end?

you want:

my ($ date) = ( $ Xml_file = ~ m / (\ d +) - sys_char /);

This will get you $ 1 in the $ date . For the second part of your question, there is no way to get all the matches counted in the same variable, but you can get them all in one array:

  my @matches = ($ Xml_file = ~ m / (\ d +) - sys_char /);  

These are actually the same syntax: When there is an array on the left hand side of such a match, then an all array array is returned to


No comments:

Post a Comment