Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regexp Help

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
string = "id <xx> MM blah blah"

'MM' is a optional value in string ...

If 'MM' exist, I want to capture contents seperately both before and after 'MM'. If 'MM' doesn't exist, just capture the entire string.

examples:
If 'MM' exist ...

/(.*)M{2}(.*)/
$1 = 'id <xx> '
$2 = ' blah blah'
This is ok...but obviously I have to account if 'MM' is not in string.

I've tried ...

/(.*)M{0,2}(.*)/
/(.*(?=MM))M{0,2}(.*)/

Can someone enlighten me and stop my head banging ...?
Thanks ...





/

 
/([^M]*)(M+)?(.*)/

I think this works ... but needs tightening up ...

Like
/([^M{2}]*)(M{2})?(.*)/


 
you could do something like this unless speed is important. Using $` and $' tend to slow things down.
Code:
# $` returns everything before the matched string. $'  returns everything after the matched string.

$string = "id <xx> MM blah blah";

if ($string =~ /\bMM\b/ ) {
   $before = $`;
   $after  = $';
}
else {
    # do something else
}
print "$before\n$after";

And don't be afraid to break up regexs into a few simpler ones if they get too complicated. You'll spend less time coding, and other people will have an easier time understanding them.
 
And if we stopped using regexes where they are not useful?
This will be much faster (and simpler to understand):
Code:
my($start,$end)=split/MM/,$string;
If there is no 'MM' [tt]$end[/tt] will be [tt]undef[/tt] and [tt]$start[/tt] will be the same as [tt]$string[/tt].

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thanks guys for the responses ...

I was just trying to exclude a repeating char class,
something like this:

string: 'ABC 123 MM'

/([M]{2})+/ ## returns 'MM'

but something like this doesn't work:
/([^M]{2})+/ ## expecting 'ABC 123 '






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top