Regex expression to split a line to three segments in Perl -


I am parsing a text file and I get multiple rows in the form shown below.

Then I try to split each line into three sections: Part 1: SF; Part 2: Name; Part 3: Directions.

But now I have to face difficulty in writing my regular expression I have thought of using an array to split on white bars and add new strings: S15, F49 large recipe Download Request (LRDR) S, H-> E, My ($ SF, $ name, $ direction) = ~ I do not know how to implement / S15, F49 // How to get other such as S1, F11 ; S6, F1 ;

$ name = Large Recipe Download Request (LRDR) // Separate names for different $ sf.

$ direction = S, H - & gt; E, answer; // It is sometime that M, H & LT; -E, Answer or S, H & lt; - & gt; E or s, h-> e, [answer] , etc. There is no white space between the sub item for part 3: $ direction

if any << If there is no white space inside the code> $ sf and $ direction items, then you can apply the following code in each row:

  if ($ Theme = ~ m / ^ (\ S +) \ s + (. *?) \ S + (\ S +) $ /) {$ sf = $ 1; $ Name = $ 2; $ Direction = $ 3; } And {// no mail found}  

Explanation:

^ : on regex to angeor The beginning of the string.

(\ S +) : Match one or more non-spacing characters. Capture the match in $ 1 .

\ s + : Match one or more space characters (= separator (next item).

(. *?) : Match as few characters as possible, however, allow overall match to be successful, and capture in $ 2 *

\ s + (\ S +) : the space separator and non-space character of the above matches -> $ 3 .

$ : Anchor search at the end of the string.


* lazy quantifier * is that otherwise, All the following space separators will be captured except for the previous one.


Comments