例子: 这只是一个简单的句子。

我想匹配这句话和句子之间的每个字符。换行符应该被忽略。我想不出正确的语法。


当前回答

我有这个字符串

      headers:
        Date:
          schema:
            type: string
            example: Tue, 23 Aug 2022 11:36:23 GMT
        Content-Type:
          schema:
            type: string
            example: application/json; charset=utf-8
        Transfer-Encoding:
          schema:
            type: string
            example: chunked
        Connection:
          schema:
            type: string
            example: keep-alive
        Content-Encoding:
          schema:
            type: string
            example: gzip
        Vary:
          schema:
            type: string
            example: Accept-Encoding
        Server:
          schema:
            type: number
            example: Microsoft-IIS/10.0
        X-Powered-By:
          schema:
            type: string
            example: ASP.NET
        Access-Control-Allow-Origin:
          schema:
            type: string
            example: '*'
        Access-Control-Allow-Credentials:
          schema:
            type: boolean
            example: 'true'
        Access-Control-Allow-Headers:
          schema:
            type: string
            example: '*'
        Access-Control-Max-Age:
          schema:
            type: string
            example: '-1'
        Access-Control-Allow-Methods:
          schema:
            type: string
            example: GET, PUT, POST, DELETE
        X-Content-Type-Options:
          schema:
            type: string
            example: nosniff
        X-XSS-Protection:
          schema:
            type: string
            example: 1; mode=block
      content:
        application/json:

并且我想删除从单词headers:到content的所有内容,所以我写了这个正则表达式(headers:)[^]*?(content)

结果和预期的一样,这个表达式出现了多少次。

其他回答

对于JavaScript,你可以使用[^]来匹配任何字符,包括换行符。

使用带点的/s标志。匹配任何字符也可以,但应用于整个模式,JavaScript不支持内联修饰符来打开/关闭标志。

为了匹配尽可能少的字符,可以通过添加问号使量词非贪婪,并使用捕获组提取介于两者之间的部分。

This is([^]*?)sentence

请参阅regex101演示。

作为旁注,为了不匹配部分单词,你可以使用像\bThis和句子\b这样的单词边界

const s = "这只是一个简单的句子"; const regex = /这是([^]*?)句子/; Const m = s.match(正则表达式); 如果(m) { console.log (m [1]); }


JavaScript中的巡视变体是(?<=This is)[^]*?(?=sentence),你可以在JS正则表达式中检查Lookbehind的支持。

请参见关于向后查找的重要说明。

const s = "这只是一个简单的句子"; 常量regex = /(?< =这是)[^]* ?(? =句子)/; Const m = s.match(正则表达式); 如果(m) { console.log (m [0]); }

例如

(?<=This is)(.*)(?=sentence)

Regexr

我使用了向后查找(?<=)和向前查找(?=),这样“This is”和“sentence”就不包含在匹配中,但这取决于你的用例,你也可以简单地写This is(.*)句子。

这里重要的是,您激活了正则表达式引擎的“dotall”模式,以便。匹配换行符。但是如何做到这一点取决于你的正则表达式引擎。

下一件事是如果你使用。*或。*?。第一个是贪婪的,将匹配到字符串中的最后一个“句子”,第二个是懒惰的,将匹配到字符串中的下一个“句子”。

更新

Regexr

This is(?s)(.*)sentence

(?s)打开dotall修饰符,使。匹配换行符。

更新2:

(?<=is \()(.*?)(?=\s*\))

匹配你的例子“这是(一个简单的)句子”。请看Regexr

试试This is[\s\ s]*?句子,工作在javascript

我有这个字符串

      headers:
        Date:
          schema:
            type: string
            example: Tue, 23 Aug 2022 11:36:23 GMT
        Content-Type:
          schema:
            type: string
            example: application/json; charset=utf-8
        Transfer-Encoding:
          schema:
            type: string
            example: chunked
        Connection:
          schema:
            type: string
            example: keep-alive
        Content-Encoding:
          schema:
            type: string
            example: gzip
        Vary:
          schema:
            type: string
            example: Accept-Encoding
        Server:
          schema:
            type: number
            example: Microsoft-IIS/10.0
        X-Powered-By:
          schema:
            type: string
            example: ASP.NET
        Access-Control-Allow-Origin:
          schema:
            type: string
            example: '*'
        Access-Control-Allow-Credentials:
          schema:
            type: boolean
            example: 'true'
        Access-Control-Allow-Headers:
          schema:
            type: string
            example: '*'
        Access-Control-Max-Age:
          schema:
            type: string
            example: '-1'
        Access-Control-Allow-Methods:
          schema:
            type: string
            example: GET, PUT, POST, DELETE
        X-Content-Type-Options:
          schema:
            type: string
            example: nosniff
        X-XSS-Protection:
          schema:
            type: string
            example: 1; mode=block
      content:
        application/json:

并且我想删除从单词headers:到content的所有内容,所以我写了这个正则表达式(headers:)[^]*?(content)

结果和预期的一样,这个表达式出现了多少次。

我在这里搜索regex来转换这个打印语法,在Python2中的旧脚本中使用:print("string")在Python3中打印"string"。工作得很好,否则使用2to3.py进行其他转换。以下是我对其他人的解决方案:

试试Regexr.com(不工作在NP++出于某种原因):

find:     (?<=print)( ')(.*)(')
replace: ('$2')

变量:

(?<=print)( )(.*)(\n)
('$2')\n

对于标签和变量:

(?<=print)( ')(.*)(',)(.*)(\n)
('$2',$4)\n

如何替换所有打印“字符串”在Python2打印(“字符串”)为Python3?