I’m trying to write a regular expression for detecting and wrapping uppercase lines with [b]..[/b].
I have a description. This description varies, as it describes games and not all descriptions have a line consisting of only uppercase words.
How can I return a a string that has lines containing only uppercase words and wrap with those tags?
Example
BUILD THE ULTIMATE BRAWLER
Design your ultimate LEGO Minifigure hero with unique style, strategy
and personality. A cactus with an attitude? A scrappy janitor ninja
who happens to have fighting chickens? A sledgehammer-swinging clown
with a few scores to settle? Sure! Build the ultimate brawler and
level them up with unlockable content!BATTLE THROUGH EPIC LEGO THEMES
From the swashbuckling shores of Barracuda Bay, to the waterlogged
caverns of Ninjago® Seabound, to a dusty wild west saloon and the
mythical Monkie Kid jungle, all of your favorite LEGO themes are
brawlable! Collect unlockable minifigs, power-ups, and emotes along
the way.CROSS-PLAY WITH FRIENDS ACROSS PLATFORMS
Team up and brawl 4v4, party with friends, or play a battle
royale-style game mode where it’s “every-player-for-themself”. With
multiple game modes, levels have unique challenges and win conditions.
I want to return ALL of that, but with the detected uppercase lines each wrapped with [b] [/b].
My attempted pattern:
/([A-Z]+(?: [A-Z]+)+)n/mg
3
Answers
The following matches all uppercase lines, wraps lines with bbcode and returns text
We can try using
preg_replace()
here:This prints:
You were on the right path with the
m
flag, but that flag has no meaning to a pattern with no anchors (^
,$
).Using start of line and end of line anchors around a character class that contains only uppercase letters and spaces and hyphens.
Code: (Demo)
If your text may contain multibyte characters (such as accented characters), then add
u
after them
pattern modifier.Alternatively, if you want to apply bold bbcode tags to full lines that do not contain any lowercase letters, then the following pattern uses a negated character class to match one or more characters on a single line that do not contain a lowercase letter (
p{Ll}
) and no "vertical whitespaces". This is unicode/multibyte safe.Code: (Demo)