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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple regex issue

Status
Not open for further replies.

eggbaconsandwich

Programmer
Joined
Mar 5, 2015
Messages
1
Location
GB
Hi tek-tips.

Perl noob here, I have the following code for turning anything to lower case after 3 slashes :

Perl:
$input = 'tea/coffee/eggs/BACON'; 
$output = $input =~ s/^((?:.*?\/){3}(.*)$/$1.lc($2)/er;
print $output;

Question, how can I quickly reverse the application of lc and only apply it to $1 instead of $2? I.e. turn anything to lower case up until finding 3 slashes, once 3 slashes found do not alter the rest.

Thanks in advance.
 
I would do this in a much faster way not using regexes:
Perl:
$input = 'Tea/Coffee/Eggs/Bacon'; 
@spl = split(/\//,$input,4);
map{$_=lc}@spl[0..2];
$output=join'/',@spl;
print $output;

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Hi

I generally agree with Franco, but in this case I would use a regular expression :
Perl:
[navy]$input[/navy] [teal]=[/teal] [i][green]'Tea/Coffee/Eggs/Bacon'[/green][/i][teal];[/teal]
[navy]$output[/navy] [teal]=[/teal] [navy]$input[/navy] [teal]=~[/teal] [b]s[/b][fuchsia]!^((?:[^/]*/){3})!\L$1![/fuchsia]r[teal];[/teal]
[b]print[/b] [navy]$output[/navy][teal];[/teal]


Feherke.
feherke.ga
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top