dongqiuqiu4736 2015-09-28 20:27
浏览 32
已采纳

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

$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.
  • 写回答

2条回答 默认 最新

  • doudao1950 2015-09-28 20:36
    关注

    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 >)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?