Page 1 of 1

multiple lines in perl regex

Posted: Wed Oct 14, 2009 3:00 am
by arsalancheema
hi, i am trying to make a parser for separating required text from a m-script.

In that m-script i have different kind of declarations e.g.

1: par_1.1_anything = min_value;

2: any_parameter = [ 0 2 3 5;
6 9 min_v_1 10;
12 15 30 35];

3: diff.name = [ 5
min
16
max];

4: not_req = [ 11 13 15;
19 24 30;
31 33 39];

So i want to separate all those declaration single line and as well as multi-line which have any character between "=" and the end i.e. "];"

What i am doing is using while(<>) so that it read any input file and then i am using print if $_ =~ /regular expression/ ;
and then save the output in output file.

i am quite successful with single lines but i and stuck with multiple lines.
I want something which when see "[" then i search all lines for any character [a-zA-Z] until it find "];" and then if it find any character in between it print the whole declaration. :banghead:

Please help me in this matter.

Thanks a lot.

Re: multiple lines in perl regex

Posted: Wed Oct 14, 2009 9:50 am
by ridgerunner
If I understand your requirements correctly, you want a regex to match a whole declaration that may span multiple lines which has at least one alpha letter [A-Za-z] occuring between square brackets. If so, this one should do the trick:

Code: Select all

preg_match_all('/^.*?=\s*\[[^\]A-Za-z]*[A-Za-z][^\]]*\];$/m', $text, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
    # Matched text = $result[0][$i];
}

Re: multiple lines in perl regex

Posted: Tue Oct 20, 2009 9:41 am
by arsalancheema
Hi again,

I need little more help please.

How can separate or split a string e.g.,

rpm_max3=[0,20,x_min;3,25,min/2;6,35,max];

i want to split it and then and put 2 counters one on comma "," and 2nd on semicolon ";". i want output like

rpm_max3 (0,0) = (0);
rpm_max3 (0,1) = (20);
rpm_max3 (0,2) = (x_min);
rpm_max3 (1,0) = (3);
rpm_max3 (1,1) = (25);
rpm_max3 (1,2) = (min/2);
rpm_max3 (2,0) = (6);
rpm_max3 (2,1) = (35);
rpm_max3 (2,2) = (max);

it is $1 (counter1 which count semicolon, counter 2 which count comma) = ( content that will be $ something)


Thanks a lot for your help.

Keep rocking,

Arsalan