Reputation: 358
I have a string like this:
my $string = 'Respect,13,201,7,0,0,2,3.70,4,1.01,Responsibility,13,177,29,1,1,2,3.58,4,1.04,Flexibility,13,180,27,0,0,3,3.59,4,1.05,Collaboration,13,194,13,0,0,3,3.65,4,1.04,Reflection,13,187,19,1,0,3,3.62,4,1.05,Commitmentto Learning,13,183,24,0,0,3,3.61,4,1.05,Beliefin Educator Efficacy,13,177,13,0,0,20,3.35,4,1.42,SocialIntelligence,13,184,22,1,0,3,3.61,4,1.05';
How can I write a pattern to be used with s///
to replace each comma (,
) just before the \w+
(e.g., Responsibility, Flexibility, Collaboration ...
) with an ampersand (&
)'?
Upvotes: 2
Views: 2727
Reputation: 386361
To replace all commas followed by \w+
(as you asked), I recommend
s/,(?=\w)/&/g
Since all commas are followed by \w+
, the above can be simplified to
s/,/&/g
If your actual intent is to only replace the commas that are followed by letters, you want
s/,(?=\pL)/&/g
Upvotes: 2
Reputation: 37009
You can use a lookahead assertion, like this:
s/,(?=[a-z]+)/&/gi
You should replace the [a-z]+
part with a more specific pattern based on your input.
Upvotes: 3