python: multiline regular expression

前端 未结 3 986
广开言路
广开言路 2021-01-22 17:26

I have a piece of text and I\'ve got to parse usernames and hashes out of it. Right now I\'m doing it with two regular expressions. Could I do it with just one multiline regular

相关标签:
3条回答
  • 2021-01-22 17:45

    Try this:

    re.findall(r'Hello, (?P<login>[^.]+)\..+?hash: (?P<hash>[^.]+)', test_str, re.S)
    
    0 讨论(0)
  • 2021-01-22 17:54
    name_hash_pair = re.findall('Hello, ([^.]+).*?hash: ([^.]+)', test_str, re.DOTALL)
    #gives [('UserName', 'fdaf9399jef9qw0j'), ('UserName2', 'gtwnhton340gjr2g')]
    
    0 讨论(0)
  • 2021-01-22 18:02

    A simple pyparsing version:

    from pyparsing import *
    
    username = Word(alphas,alphanums+"_")
    hash = Word(alphanums)
    
    patt = ("Hello," + username("username") + '.' + 
            SkipTo("write down this hash:", include=True) + 
            hash("hash"))
    
    for tokens,start,end in patt.scanString(test_str):
        print tokens.hash, '->', tokens.username
    
    # or to build a dict
    hashNameLookup = dict((t.hash, t.username) 
                                    for t,s,e in patt.scanString(test_str))
    

    Prints:

    fdaf9399jef9qw0j -> UserName
    gtwnhton340gjr2g -> UserName2
    
    0 讨论(0)
提交回复
热议问题