?
Solved

Perl Regular Expression in UltraEdit: how to search a pattern in multiple lines

Posted on 2010-01-12
11
Medium Priority
?
1,784 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:huangs3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 13

Expert Comment

by:marchent
ID: 26294027

my $str = 'aaaaa
bbbbb
ertyu
bbbbb
ccccc
aaaaa
aaaaa
bbbbb
68999
bbbbb
ccccc
aaaaa
';
$\ = $/;
print for $str =~ /(bbbbb.*?ccccc)/sg;

Open in new window

0
 

Author Comment

by:huangs3
ID: 26299020
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
0
 
LVL 6

Expert Comment

by:zlobcho
ID: 26299559
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:zlobcho
ID: 26299607
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";
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26301394
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

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26301402
The above assumes that each line contains only the text you described above (e.g. line 5 only contains the string "ccccc")
0
 

Author Comment

by:huangs3
ID: 26308835
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
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 26309278
Ok. To get a gripe out of the way--I really don't like their syntax :(

It took me a minute to get this to work, but the options I have are visible below:
untitled.JPG
untitled1.JPG
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26309287
Of course, the above only works if there are two lines separating a "bbbbb" string and a "ccccc" string.
0
 

Author Closing Comment

by:huangs3
ID: 31676122
Even though it is not Perl style, it works. Thank you!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26319244
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.

:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question