CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
RegExp that is giving me fits

 



tanker
Novice

Mar 22, 2017, 11:04 AM

Post #1 of 5 (13082 views)
RegExp that is giving me fits Can't Post

I have spent the past couple days trying to get an expression to work and I am definitely missing something. I'm a mid level Perl user.

Source line examples:

Code
<I32>SLEEVE (USE WITH INDEX 2) 
<I31>SPACER, SLEEVE (UNDER BOLT HEAD) (USE WITH INDEX 73)


What I expect to get:

Code
(USE WITH INDEX 2) 
(UNDER BOLT HEAD) (USE WITH INDEX 73)


What I'm getting:

Code
(USE WITH INDEX 2) 
(USE WITH INDEX 73)


My current attempt is the following.

Code
$XYZ =~ s/^(.*)\((.*)\)$/\($2\)/;


I'm trying to capture the notes between the first "(" and the last ")". For some reason I can't get it to keep the first "(...)" in this case. In my current attempt I try to match the beginning of the line followed by any characters up to ( then match any text up to a ) at the end of the string. Then output the contents of the 2nd wildcard between ( ). The way I have written it, I'm thinking (obviously incorrectly) it should even catch (...) (...) (...)

I know it's going to probably be something simple but putting it aside to think about other things, hoping it comes to me, isn't working.

Thanks in advance, Stan


FishMonger
Veteran / Moderator

Mar 22, 2017, 11:26 AM

Post #2 of 5 (13080 views)
Re: [tanker] RegExp that is giving me fits [In reply to] Can't Post


Code
use 5.010; 
use warnings;
use strict;

my @lines = ('<I32>SLEEVE (USE WITH INDEX 2)',
'<I31>SPACER, SLEEVE (UNDER BOLT HEAD) (USE WITH INDEX 73)');

foreach my $line (@lines) {
say $1 if $line =~ /(\(.+\))/
}



(This post was edited by FishMonger on Mar 22, 2017, 11:27 AM)


FishMonger
Veteran / Moderator

Mar 22, 2017, 11:32 AM

Post #3 of 5 (13077 views)
Re: [tanker] RegExp that is giving me fits [In reply to] Can't Post

If you want to use/fix your substitution regex, the easiest adjustment would be to make the first group to be non-greedy.


Code
s/^(.*?)\((.*)\)$/\($2\)/;



BillKSmith
Veteran

Mar 23, 2017, 7:39 PM

Post #4 of 5 (13059 views)
Re: [tanker] RegExp that is giving me fits [In reply to] Can't Post

Do you want to match everything from the first open parentheses to the last close parenthesis? Try this


Code
use strict; 
use warnings;

my @lines = (
'<I32>SLEEVE (USE WITH INDEX 2)',
'<I31>SPACER, SLEEVE (UNDER BOLT HEAD) (USE WITH INDEX 73)'
);
foreach my $line (@lines) {
$line =~ s/.*?(\(.*)\)/$1/;
print $line, "\n";
}

OUTPUT:
(USE WITH INDEX 2)
(UNDER BOLT HEAD) (USE WITH INDEX 73)

Good Luck,
Bill


tanker
Novice

Mar 24, 2017, 6:30 AM

Post #5 of 5 (13048 views)
Re: RegExp that is giving me fits [In reply to] Can't Post

Thanks for the reply to you both. Sorry I didn't reply earlier, been one of those weeks. Boss man, needed something urgently (ain't that always the case Smile) and had to put everything else on hold.

I guess I should have mentioned but didn't know how much to include in my question. The substitution was already within a while loop that reads the line from a file.

FishMonger your second reply is exactly what I was looking for. Thank you. The only change I made after testing it was to replace the "$" at the end of the match string with a "\n" so that it removes the new line.

Thanks again guys,
Stan

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives