Solved

Perl regex to replace any capital letters not preceded by ">"

Posted on 2016-08-04
6
177 Views
Last Modified: 2016-08-08
Argh... been going in circles.  Can someone please provide a Perl regex to replace all capital letters in a string that are not preceded by ">"

e.g. <b>A</b>ll Good Boys <b>D</b>eserve Favor

I want to surround the G, B and F with <b> and </b> like the other capital letters

Thanks-
0
Comment
Question by:SAbboushi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 450 total points
ID: 41743302
$subject = '<b>A</b>ll Good Boys <b>D</b>eserve Favor';
$subject =~ s![^>]([A-Z])!<b>$1</b>!g;
or
$subject =~ s!([A-Z])[^<]!<b>$1</b>!g;

HTH,
Dan
0
 
LVL 5

Accepted Solution

by:
foochar earned 50 total points
ID: 41743385
To generalize the solution you have to protect against some edge cases as well.  The examples provided by the previous commenter break if the when looking at the first and last character of the string, as there is not a preceding (or succeeding in the second example) character to match the [^<].  To work around this the first solution I came up with was:

s/(^|[^>])([A-Z])/$1<b>$2<\/b>/g

Open in new window


When I tested this I realized however that it doesn't work so well when it encounters a consecutive all caps and the </b> tag makes it skip the next letter.  It would also "miss" a capital proceeded by another tag such as <i> or </i>.  To work around this I used the "look back" functionality to come up with the following regex:

s/(?<!<b>)([A-Z])/<b>$1<\/b>/g

Open in new window


By using the ?<! negative lookbehind it only matches cases where the capital letter is not proceeded by the <b> tag.  It also eliminates the need to specifically catch the edge case of the beginning of the string because when it tries to look back when on the first character there is nothing there, and therefore it satisfies the negative lookbehind.  Thanks to the information at http://www.regular-expressions.info/lookaround.html for clarifying some of the look around specifics for me...
1
 

Author Closing Comment

by:SAbboushi
ID: 41746367
Thanks folks!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SAbboushi
ID: 41746369
btw - you gave me exactly what I asked for.  Would be grateful if you don't mind amending it to work only on word boundaries so the "D" isn't a match in Ph.D.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 41746378
[^>]([A-Z])(?=[a-z ].)

Will only match if the capital letter is followed by a regular letter.

Edit: allowed a space after the capital letter, to allow "I ".
0
 

Author Comment

by:SAbboushi
ID: 41747978
k thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question