简体   繁体   中英

extracting regular expression from string

Is, there a better way to extract the strings :

  'Found 1 items\ndrwxr-xr-x   - hadoop supergroup          0 2013-02-16 13:21 /user/hadoop/wiki\n'

All the strings will be like:

  'Found **n** items\n**permissions**   - **username** **group**          **notsurewhatthisis** **date** **time** **folders(or file)**\n'

Right now.. i am splitting it as:

line = line.split()
num_items = int(line[1])
permissions = line[3]

etc..

So basically this is a no brainer solution..

Trying to see if there is a "python" way to do this.

ss = ('Found 1 items\n'
      "drwxr-xr-x   - hadoop supergroup          "
      '0 2013-02-16 13:21 /user/hadoop/wiki\n')

('Found **n** items\n'
 '**permissions**   - **username** **group**          '
 '**notsurewhatthisis** **date** **time** **folders(or file)**\n')

import re

r = re.compile('Found +(\d+) +items *\n *(.+?) *- ')

print r.search(ss).groups()

ss is a string
'Found +(\\d+) +items *\\n *(.+?) *- ' is a string used as a pattern for creating a regular expression object
r is the regular expression, an object which is not a string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM