huangs3
asked on
Perl Regular Expression in UltraEdit: how to search a pattern in multiple lines
Hi Experts:
I want to use a regular expression to help me replace repeating multi-line SQL statements sections for me. From some webpage I was instructed that the perl style regular expression in UltraEdit software can help me to do multi-line regular expression search, but I haven't figured out how to do it yet. Please help me to find the regular expression to match lines 2-5, and 8-11 in the following text file. It will help me to get some understanding.
Thank you!
line 1: aaaaa
line 2:bbbbb
line 3:ertyu
line 4:bbbbb
line 5:ccccc
line 6:aaaaa
line 7:aaaaa
line 8:bbbbb
line 9:68999
line10:bbbbb
line11:ccccc
line12:aaaaa
I want to use a regular expression to help me replace repeating multi-line SQL statements sections for me. From some webpage I was instructed that the perl style regular expression in UltraEdit software can help me to do multi-line regular expression search, but I haven't figured out how to do it yet. Please help me to find the regular expression to match lines 2-5, and 8-11 in the following text file. It will help me to get some understanding.
Thank you!
line 1: aaaaa
line 2:bbbbb
line 3:ertyu
line 4:bbbbb
line 5:ccccc
line 6:aaaaa
line 7:aaaaa
line 8:bbbbb
line 9:68999
line10:bbbbb
line11:ccccc
line12:aaaaa
ASKER
Hi marchent:
Thank you for your Perl code. in UltraEdit i tried /(bbbbb.*?ccccc)/sg as the searching string, but couldn't match them.
I found this example telling what to do in UltraEdit http://stackoverflow.com/questions/1441044/multiline-regular-expression-search-and-replace
but still couldn't understand it.
Can you help me to find out what shall I put into the UltraEdit search box?
Thank you!
Sui
Thank you for your Perl code. in UltraEdit i tried /(bbbbb.*?ccccc)/sg as the searching string, but couldn't match them.
I found this example telling what to do in UltraEdit http://stackoverflow.com/questions/1441044/multiline-regular-expression-search-and-replace
but still couldn't understand it.
Can you help me to find out what shall I put into the UltraEdit search box?
Thank you!
Sui
do you need something like this:
#!/usr/bin/perl
$/="^\$";
while(<DATA>)
{
$_=~/.*(line 2.*)line 6.*(line 8.*)line12.*/sm;
print $1;
print $2;
}
__DATA__
line 1: aaaaa
line 2:bbbbb
line 3:ertyu
line 4:bbbbb
line 5:ccccc
line 6:aaaaa
line 7:aaaaa
line 8:bbbbb
line 9:68999
line10:bbbbb
line11:ccccc
line12:aaaaa
#!/usr/bin/perl
$/="^\$";
while(<DATA>)
{
$_=~/.*(line 2.*)line 6.*(line 8.*)line12.*/sm;
print $1;
print $2;
}
__DATA__
line 1: aaaaa
line 2:bbbbb
line 3:ertyu
line 4:bbbbb
line 5:ccccc
line 6:aaaaa
line 7:aaaaa
line 8:bbbbb
line 9:68999
line10:bbbbb
line11:ccccc
line12:aaaaa
Or this here::
#!/usr/bin/perl
$/="^\$";
open (DATA,"<textfile.txt") or die;
while(<DATA>)
{
$change1="change1\n";
$change2="change2\n";
$_=~/.*(line 2.*?)line 6.*/sm;
$_=~s/$1/$change1/;
$string=$_;
$_=~/.*(line 8.*?)line12.*/sm;
$_=~s/$1/$change2/;
$string=$_;
}
print $string."\n";
#!/usr/bin/perl
$/="^\$";
open (DATA,"<textfile.txt") or die;
while(<DATA>)
{
$change1="change1\n";
$change2="change2\n";
$_=~/.*(line 2.*?)line 6.*/sm;
$_=~s/$1/$change1/;
$string=$_;
$_=~/.*(line 8.*?)line12.*/sm;
$_=~s/$1/$change2/;
$string=$_;
}
print $string."\n";
Make sure you have the Perl radio button checked:
lines 2-5:
bbbbb\nertyu\nbbbbb\nccccc
lines 8-11:
bbbbb\n68999\nbbbb\nccccc
In the above, \n matches a new line
The above assumes that each line contains only the text you described above (e.g. line 5 only contains the string "ccccc")
ASKER
Hi kaufmed:
I tried the first regular expression but it didn't work. The screen shot shows the options I selected. Can you help me to check what should be changed?
Also how can I get one regular expression to match both 2-5 and 8-11 in the example? Wild card must be allowed in the expression.
Thank you!
UeTest.jpg
I tried the first regular expression but it didn't work. The screen shot shows the options I selected. Can you help me to check what should be changed?
Also how can I get one regular expression to match both 2-5 and 8-11 in the example? Wild card must be allowed in the expression.
Thank you!
UeTest.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Of course, the above only works if there are two lines separating a "bbbbb" string and a "ccccc" string.
ASKER
Even though it is not Perl style, it works. Thank you!
Yeah, that's why I was griping. I am much fonder of Perl syntax (mainly because that's how I learned regexes). My apologies for the Unix version, but I was hopeful it would at least get you by.
:)
:)
Open in new window