Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果特定模式匹配,则使用正则表达式检索行中的所有文本_Python_Regex - Fatal编程技术网

Python 如果特定模式匹配,则使用正则表达式检索行中的所有文本

Python 如果特定模式匹配,则使用正则表达式检索行中的所有文本,python,regex,Python,Regex,我试图编写一个正则表达式,以便在有特定模式匹配的情况下获取一行的所有内容。我要在一行中搜索的字符串类似于: 1. 7.2.S.6.4 ANNOTATED DATA OR 2. 9-2-K-1-4 FILE DATA OR 3. 2-2.K-4.3 FOLDER DATA 从这些案例中,我希望得到以下输出: 注释数据 文件数据 文件夹数据 我想编写一个正则表达式来找出第一个模式示例:“7.2.S.6.4”,然后从该模式后面的行中获取下一个单词 到目前为止,我试过的正则表达式是

我试图编写一个正则表达式,以便在有特定模式匹配的情况下获取一行的所有内容。我要在一行中搜索的字符串类似于:

1. 7.2.S.6.4 ANNOTATED DATA 
    OR
2. 9-2-K-1-4 FILE DATA
    OR
3. 2-2.K-4.3 FOLDER DATA
从这些案例中,我希望得到以下输出:

  • 注释数据
  • 文件数据
  • 文件夹数据
  • 我想编写一个正则表达式来找出第一个模式示例:“7.2.S.6.4”,然后从该模式后面的行中获取下一个单词

    到目前为止,我试过的正则表达式是

    \s*(-?\d+(?:\.\d+)?)
    

    但是它和S不匹配。或者,模式中的-K-部分。任何关于如何解决这个问题的想法对我来说都有点神秘,但这可能适用于第一个匹配,尽管它不是最理想的解决方案:

    \s*([-.]?\d+(?:\.\d+)?([-.][A-Z])?)[ ](.*)
    

    您可以使用这样的正则表达式:

    ^(\d\.) \S+(.*)
    
    然后从捕获组1和2中获取内容

    此外,您还可以使用此正则表达式,并将
    $1$2
    用作替换字符串:

    ^(\d\.) \S+(.*)|.+
    

    示例代码

    import re
    
    regex = r"^(\d\.) \S+(.*)|.+"
    
    test_str = ("1. 7.2.S.6.4 ANNOTATED DATA \n"
        "    OR\n"
        "2. 9-2-K-1-4 FILE DATA\n"
        "    OR\n"
        "3. 2-2.K-4.3 FOLDER DATA")
    
    subst = "$1$2"
    
    # You can manually specify the number of replacements by changing the 4th argument
    result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
    
    if result:
        print (result)
    

    这些表达可能在这里起作用

    (?=[0-9]+[.-][0-9]+[.-][A-Z]+[.-][0-9]+[.-][0-9]+).*[0-9]\s(.+)
    (?=[0-9]+[.-][0-9]+[.-][A-Z]+[.-][0-9]+[.-][0-9]+).*[0-9]\s+(.+)
    
    这个隔间确保我们有一个正确的模式

    (?=[0-9]+[.-][0-9]+[.-][A-Z]+[.-][0-9]+[.-][0-9]+)
    
    在这里,我们将捕获所需的输出

    (.+)
    
    试验
    使用此网站查找正确的正则表达式Try
    re.sub(r'^\d+\.\s+\d+(?:[.-]\w+*\s*','',s)
    ,查看数据是否如示例所示:
    l=s.split('');res=l[0]+l[2://code>为什么不逐行阅读并分别测试每一行的匹配项?这样可以减少灾难性回溯的风险。
    
    # coding=utf8
    # the above tag defines encoding for this document and is for Python 2.x compatibility
    
    import re
    
    regex = r"(?=[0-9]+[.-][0-9]+[.-][A-Z]+[.-][0-9]+[.-][0-9]+).*[0-9]\s(.+)"
    
    test_str = ("7.2.S.6.4 ANNOTATED DATA\n"
        "9-2-K-1-4 FILE DATA\n"
        "2-2.K-4.3 FOLDER DATA")
    
    matches = re.finditer(regex, test_str, re.MULTILINE)
    
    for matchNum, match in enumerate(matches, start=1):
    
        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
        for groupNum in range(0, len(match.groups())):
            groupNum = groupNum + 1
    
            print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
    
    # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.