Solved

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

Posted on 2016-08-04
6
140 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 34

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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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 34

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

829 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