Solved

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

Posted on 2016-08-04
6
97 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
Comment Utility
$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
Comment Utility
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
Comment Utility
Thanks folks!
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:SAbboushi
Comment Utility
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
Comment Utility
[^>]([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
Comment Utility
k thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now