Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1830
  • Last Modified:

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
0
huangs3
Asked:
huangs3
  • 5
  • 3
  • 2
  • +1
1 Solution
 
marchentCommented:

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
 
huangs3Author Commented:
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
 
zlobchoCommented:
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
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.

 
zlobchoCommented:
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
 
käµfm³d 👽Commented:
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
 
käµfm³d 👽Commented:
The above assumes that each line contains only the text you described above (e.g. line 5 only contains the string "ccccc")
0
 
huangs3Author Commented:
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
 
käµfm³d 👽Commented:
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
 
käµfm³d 👽Commented:
Of course, the above only works if there are two lines separating a "bbbbb" string and a "ccccc" string.
0
 
huangs3Author Commented:
Even though it is not Perl style, it works. Thank you!
0
 
käµfm³d 👽Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now