之间如何解析!!!和 ???使用正则表达式
How to parse between !!! and ??? using regex
我有一串如下所示的文本:
Image 56 ::: Beach, Summer, Sun !!! John Smith ???
我只想提取 John Smith
我试过了(?s)(?<=\!\!\!).+?(?=\?\?\?})
和(\!\!\!)(?:[^])*?(\?\?\?)
但是第一个不起作用,第二个给出的结果 !!! John Smith ???
不仅仅是 John Smith。你能提供一些帮助吗?谢谢
您的第一个模式中有一个杂散的 }
字符。删除它,它将起作用。
试试这个:
!!!\s+\K.*?(?=\s+\?{3})
勾选https://regex101.com/r/uF0gV8/1 and Support of \K in regex
解释:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
!!! '!!!'
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\K restart the match
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
----------------------------------------------------------------------
\?{3} '?' (3 times)
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
试试这个:
!!!\s*\K[^\n\r?]+?(?=\s*\?\?\?)
您可以使用带有否定字符的捕获组 class。
!!!\s*([^?]+?)\s*\?{3}
要显示匹配结果,您需要引用第一组索引。
Live Demo
我有一串如下所示的文本:
Image 56 ::: Beach, Summer, Sun !!! John Smith ???
我只想提取 John Smith
我试过了(?s)(?<=\!\!\!).+?(?=\?\?\?})
和(\!\!\!)(?:[^])*?(\?\?\?)
但是第一个不起作用,第二个给出的结果 !!! John Smith ???
不仅仅是 John Smith。你能提供一些帮助吗?谢谢
您的第一个模式中有一个杂散的 }
字符。删除它,它将起作用。
试试这个:
!!!\s+\K.*?(?=\s+\?{3})
勾选https://regex101.com/r/uF0gV8/1 and Support of \K in regex
解释:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
!!! '!!!'
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\K restart the match
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
----------------------------------------------------------------------
\?{3} '?' (3 times)
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
试试这个:
!!!\s*\K[^\n\r?]+?(?=\s*\?\?\?)
您可以使用带有否定字符的捕获组 class。
!!!\s*([^?]+?)\s*\?{3}
要显示匹配结果,您需要引用第一组索引。