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!

Problem with minimal (nongreedy) matching 1

Status
Not open for further replies.

sloppyhack

Technical User
Apr 17, 2001
111
US
I'm trying to truncate a string at a non-alphanumeric character to include the most characters to still be within a 30 character maximum. It's being greedy even though I'm using the "?" non-greedy version of the quantifier.

Here's the string....

BIT|DRL|6MM|12.8CM|CTR|CNRSTN-SR AM|D11MM|SPNL|DISP

Here's the regex....

/[^a-z0-9].{21,}?$/i

It's matching on "|DRL|6MM|12.8CM|CTR|CNRSTN-SR AM|D11MM|SPNL|DISP". I want it to match on "|CNRSTN-SR AM|D11MM|SPNL|DISP". I can't get it to do a non-greedy match while archoring the expression the end of the string?

Cheers,

Sloppyhack
 
I'm not sure this is the best way to do what you want, but you need to change the quantifier {21,} to {0,30} or {1,30} or whatever minimum length is appropriate.

Code:
my $foo = 'BIT|DRL|6MM|12.8CM|CTR|CNRSTN-SR AM|D11MM|SPNL|DISP';
(my $bar) = $foo =~ /([^a-z0-9].{0,30}?)$/i;
print $bar;

I don't think you really need the "?" since you are using the {,} quantifier to tell the regexp how much stuff to match.
 
I figured it out. I'm using ^(.{0,30})[^a-z0-9\/] and using the text that was captured within the parens as my new string. I can't seem to use "non-greedy" while archoring to the end of the string. Works if achoring from the front. Don't need it with the expression I'm using though.

Thanks for he suggestion. It got me to the solution.

Cheers,

Sloppyhack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top