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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
Regex 是否有一种方法可以根据固定条件将动态字符串分组_Regex - Fatal编程技术网

Regex 是否有一种方法可以根据固定条件将动态字符串分组

Regex 是否有一种方法可以根据固定条件将动态字符串分组,regex,Regex,示例字符串: sjdjd|shjdjd,A|shdahdajh,E|hsdhsdj,C|asnma,A shabnmun|sjddamd,E|wqhsbn,E,teyiuiw,S|ssbxnsmcn,A|vscnbdcmdn,E sanbsnk|scbdcbd,A 我想创建5个组: 包括第一个示例中第一个|-“sjdjd”前面的第一个字符串 在分隔的部分中包含、A的所有字符串 在分隔的部分中包含,E的所有字符串 在分隔的部分中包含,C的所有字符串 在分隔的部分中包含、S的所有字符串 我尝试的模

示例字符串:

sjdjd|shjdjd,A|shdahdajh,E|hsdhsdj,C|asnma,A
shabnmun|sjddamd,E|wqhsbn,E,teyiuiw,S|ssbxnsmcn,A|vscnbdcmdn,E
sanbsnk|scbdcbd,A
我想创建5个组:

  • 包括第一个示例中第一个
    |
    -“sjdjd”前面的第一个字符串
  • 在分隔的部分中包含
    、A
    的所有字符串
  • 在分隔的部分中包含
    ,E
    的所有字符串
  • 在分隔的部分中包含
    ,C
    的所有字符串
  • 在分隔的部分中包含
    、S
    的所有字符串
  • 我尝试的模式是:

    ^(?:.+)\|(.+)\|(?:.+)\|(?:.+)\|(?:.+)+$
    
    预期群体


    (第1组-沙班孟sjdjd,sanbsnk),(第2组-沙班孟sjdjd,asnma, 西昆士兰西达姆德sjddamd shdahdajh第3组, vscnbdcmdn),(第4组-hsdhsdj),(第5组-teyiuiw)


    最简单的解决方案是只需执行多个搜索和组装:

    import re
    
    s = """sjdjd|shjdjd,A|shdahdajh,E|hsdhsdj,C|asnma,A
    shabnmun|sjddamd,E|wqhsbn,E,teyiuiw,S|ssbxnsmcn,A|vscnbdcmdn,E
    sanbsnk|scbdcbd,A"""
    
    l1 = re.findall(r'^[^|]+', s, flags=re.MULTILINE)
    lA = re.findall(r'(?<=[|,])[^|,]+(?=,A)', s)
    lE = re.findall(r'(?<=[|,])[^|,]+(?=,E)', s)
    lC = re.findall(r'(?<=[|,])[^|,]+(?=,C)', s)
    lS = re.findall(r'(?<=[|,])[^|,]+(?=,S)', s)
    
    groups = [l1, lA, lE, lC, lS]
    for group in groups:
        print(group)
    

    最简单的解决方案是只需进行多个搜索和组装:

    import re
    
    s = """sjdjd|shjdjd,A|shdahdajh,E|hsdhsdj,C|asnma,A
    shabnmun|sjddamd,E|wqhsbn,E,teyiuiw,S|ssbxnsmcn,A|vscnbdcmdn,E
    sanbsnk|scbdcbd,A"""
    
    l1 = re.findall(r'^[^|]+', s, flags=re.MULTILINE)
    lA = re.findall(r'(?<=[|,])[^|,]+(?=,A)', s)
    lE = re.findall(r'(?<=[|,])[^|,]+(?=,E)', s)
    lC = re.findall(r'(?<=[|,])[^|,]+(?=,C)', s)
    lS = re.findall(r'(?<=[|,])[^|,]+(?=,S)', s)
    
    groups = [l1, lA, lE, lC, lS]
    for group in groups:
        print(group)
    

    请根据示例字符串向我们显示您想要的结果。(第1组-sjdjd、shabnmun、sanbsnk),(第2组-sjdjd、asnma、ssbxnsmcn、scbdcbd),(第3组-Shdahdjh、sjddamd、wqhsbn、vscnbdcmdn),(第4组-hsdhsdj),(第5组-teyiuiw)请编辑问题并向我们显示您想要的所有组,在注释中添加了预期的组。您使用的语言/工具是什么?请根据示例字符串向我们显示您想要的结果。(组1-sjdjd、Shannmun、sanbsnk),(组2-Shjdd、asnma、ssbxnsmcn、scbdcbd),(组3-shdahdajh、sjddamd、wqhsbn、vscnbdcmdn),(组4-hsdhsdj),(组5-teyiuiw)请编辑问题并向我们显示您想要的所有组,以及您想要的组。在评论中添加了预期的组您使用的是什么语言/工具?