考虑一个字符串:
<! Women's boxing is yet to be recognized as an Olympic support,----. If that happens the dream of most of the tough girls may come true. !> <! dream of most of the tough girls may come true. !> <! Women's boxing is yet to be recognized as an Olympic support. !>在上面的字符串中,我使用了<!和!>作为指定段落边界的分隔符,从字符串中可以明显看出,我在分隔符中包含了3+1组封闭语句。
我用来捕获组的正则表达式是
<!([^<!>].+)!>它匹配的组是:
Group 1: <! Women's boxing is yet to be recognized as an Olympic support,----. If that happens the dream of most of the tough girls may come true. !> <! dream of most of the tough girls may come true. !> <! Women's boxing is yet to be recognized as an Olympic support. !>
Group 2: Women's boxing is yet to be recognized as an Olympic support,----. If that happens the dream of most of the tough girls may come true. !> <! dream of most of the tough girls may come true. !> <! Women's boxing is yet to be recognized as an Olympic support. 我假设匹配包括三个内部组,但它匹配它只包括外部组作为输出。这是我所期望的
//other groups
Women's boxing is yet to be recognized as an Olympic support,----. If that happens the dream of most of the tough girls may come true.
dream of most of the tough girls may come true.
Women's boxing is yet to be recognized as an Olympic support.发布于 2021-03-17 00:56:23
模式<!([^<!>].+)!>从与除<、!或>之外的任何字符匹配的组中的第一个字符开始。然后,.+将一直匹配到行的末尾,并回溯到第一次遇到!>
如果您不想捕获空格字符,可以匹配开头和结尾的空格字符,并使用非空格字符\S启动捕获组,而不仅仅是匹配空格字符。
然后使用延迟匹配,直到第一次出现!>
<!\s*(\S.*?)\s*!>https://stackoverflow.com/questions/66651324
复制相似问题