?
Solved

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

Posted on 2016-08-04
6
Medium Priority
?
242 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
  • 3
  • 2
6 Comments
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 1800 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans
Suggested Courses

850 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