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

regex question : ignoring first match in pattern search

Status
Not open for further replies.

bcdixit

Technical User
Nov 11, 2005
64
US
I have the following piece of code,

#!/usr/bin/perl

$txt = "xxx yys";
$txt =~ s/\b/\!/g;
print "$txt\n";

basically I want to substitute at a word boundary a '!' character but not on the first word of the string or line.

the output that I am currently getting is

!xxx! !yys!

I want it to look something like this.

xxx! !yys!

Ideally I want it too be something like xxx!yys.

But I would highly appreciate if someone could give a hint of at least ignoring the first word boundary.

thanks
bcd
 
This will not do the replacement if it's at the start of the string:
Code:
$txt =~ s/(?<!^)\b/\!/g;
 
If you want to just add exclamation marks at the end of each word, you could do:
Code:
$txt =~ s/(\w)\b/$1!/g;

This leaves you with an exlamation mark at the end of each line, but since this will always be the case, you might easily add a
Code:
$txt =~ s/!$//;

Maybe you can also do it in one go with some look-ahead assertion, but matching the end of the srting ($) in a look-ahead is a bit tricky I find...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top