I am parsing a text file and I get multiple lines in the form shown below.
Then I try to split each line to three segments: Part1: sf; part2: name; part3:direction.
But now I am encountering difficulty in how to write my regular expression. I have thought about splitting on whitespace and using an array to concatenate new strings:
S15,F49 Large Recipe Download Request (LRDR) S,H->E,reply
my ($sf, $name, $direction) =~ / I don't know how to implement here/
How can I get $sf = S15,F49 // other lines like S1,F11; S6,F1; etc
$name = Large Recipe Download Request (LRDR) // different name for different $sf.
$direction = S,H->E,reply; // some time it is M,H<-E,reply or S,H<->E or S,H->E,[reply], etc. There is no white space between each of sub items for part3: $direction
If there is no whitespace within the
$sfand the$directionitems, then you could apply the following code to each line:Explanation:
^: Anchor the regex at the start of the string.(\S+): Match one or more non-space characters. Capture the match in$1.\s+: Match one or more space characters (= separator(s) to the next item).(.*?): Match any number of characters, as few as possible to still allow the overall match to succeed, and capture that in$2.*\s+(\S+): Similar to the above – match space separator(s) and non-space characters –>$3.$: Anchor the search at the end of the string.*The reason for the lazy quantifier
*?is that otherwise, this part of the regex would also capture all the following space separators except the last one.