正则表达式跳过里面小于标志和大于标志

$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

  • a "normal" character (something that's not <)

or

  • a <...> 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.