good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Print text on the same line after matchby haukex (Archbishop) |
on Dec 14, 2018 at 21:00 UTC ( [id://1227269]=note: print w/replies, xml ) | Need Help?? |
I have only been able to come up with the match regex, but I'm not sure how to get the text at the end. Unless you're really concerned about performance, it doesn't hurt to split things up into two regexes to make things easier to think about, for example you could say:
But in this case it's not too difficult to do it in a single regex, see below. (Update: To answer your question more basically, have a look at perlretut, where capturing groups are explained.) Inside my perl script I assume that I will use something like this It's almost never necessary to call perl from inside a Perl script. The while loop in the following code is the equivalent of a one-liner. I've added some checks to make sure that the number is found exactly once in the input file - I use defined instead of a boolean check to differentiate between 0, which Perl would consider false, and undef, which in this case means that no number was found. In the regex, I'm using a couple of techniques: /x for readability, and a negative lookbehind (?<!\d) to make sure the thing before the number is not also a number - not really required in this case (it's a bit of paranoia), but it could become an important technique if your match happens to get more complex. You didn't say whether there could be any characters in the string between "INC*XX*" and the number; if not, then the regex could be simplified to just /INC\*XX\*(\d+)~$/.
In Section
Seekers of Perl Wisdom
|
|