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

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


当前回答

我有这个字符串

      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)

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

其他回答

这适用于我(我使用VS Code):

: 这只是一个简单的句子

使用: +句子

RegEx使用Java方法匹配两个字符串之间的所有内容。

List<String> results = new ArrayList<>(); //For storing results
String example = "Code will save the world";

让我们使用Pattern和Matcher对象来使用RegEx(.?)*。

Pattern p = Pattern.compile("Code "(.*?)" world");   //java.util.regex.Pattern;
Matcher m = p.matcher(example);                      //java.util.regex.Matcher;

由于Matcher可能包含多个匹配项,我们需要遍历结果并存储它。

while(m.find()){   //Loop through all matches
   results.add(m.group()); //Get value and store in collection.
}

这个例子将只包含“will save the”这个单词,但是在更大的文本中,它可能会找到更多匹配项。

我在这里搜索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?

这样的:

This is (.*?) sentence

工作在javascript。

需要惰性量词

重新提出这个问题是因为公认答案中的正则表达式对我来说似乎不太正确。为什么?因为

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

会和我的第一句话相匹配。这是我的第一句话。这是我的第二句话。

看到演示。

在两个环视之间需要一个惰性量词。增加一个?使恒星变懒。

这是你想要的:

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

看到演示。我删除了不需要的捕获组。

DOTALL模式匹配跨换行

注意,在演示中设置了“点匹配换行模式”(a.k.a)点-all(参见如何在各种语言中打开DOTALL)。在许多正则表达式中,您可以使用在线修饰符(?s)设置它,将表达式转换为:

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

参考

正则表达式贪婪的多种程度 星号和加号重复