$regLinks = "~meaning+?.{0,500}\\.~siU";
I need the last period, the \\\\.
to be not inside of less than sign, and the greater than sign <>
. So something like <color blue.>
would be skipped over. How would I achieve that in regex?
$string "meaning: sad is when you are unhappy <blue green.> right now.";
^---So out of this, instead of stopping at <blue green.>
, it should stop at
meaning: sad is when you are unhappy `<blue green.>` right now.
You could change the .
in .{0,500}
to (?:[^<]|<[^>]*>)
.
(?: )
is a regex group that doesn't capture (plain ( )
would also capture the string it matched).
<
and >
simply match themselves.
[^>]*
matches 0 or more non- >
characters.
In effect instead of matching "any character" ( .
), we match either
<
) or
<...>
group (which consists of a <
, followed by 0 or more non- >
characters, followed by >
) Try this:
$regLinks = "~meaning+?(?:[^<]|<[^>]*>){0,500}\\.~siU";
I kept the {0,500}
bit because I figured you had a reason for that, though it would be slightly more efficient to write:
$regLinks = "~meaning+?(?:[^<]+|<[^>]*>){0,500}\\.~siU";
However, that could consume arbitrary many characters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.