简体   繁体   中英

Regular expressions, using an expression in Python with a space involved

I generated this expression, after reading the documentation:

(\['[^']+'(?:,\s*'[^']+'){0,}\]) (\[(?:'[^']+')(?:,\s*'[^']+'){0,}\]) ('[^']+') ([1-9][0-9]*)

It is meant to recognise such pattern:

['1','11','111'] ['cpp','h'] 'utf-8' 500

I tried to use it in Python 3.5.4:

import re
import sys
x = sys.argv[1:]
args = re.match(u"(\['[^']+'(?:,\s*'[^']+'){0,}\]) (\['[^']+'(?:,\s*'[^']+'){0,}\]) ('[^']+') ([1-9][0-9]*)",
                ' '.join(x))

I can recognize this example:

['1','11','111'] ['cpp','h'] 'utf-8' 500

However when trying to recognize this:

['1', '11', '111'] ['cpp', 'h'] 'utf-8' 500

or this:

['1','1 1','1 11'] ['cp p','h'] 'utf-8' 500

in python it fails, whenever a space is represented in between (') and (') or between (',) and (') .

But on this site the regex works like a charm.

Any idea why is this so?

I am soory, noob's mistake, first project in python.

I totaly forgot that in cmd I should include text using ' " ' whenever it contains spaces.

wrong way:

python re-encoder.py ['1','11','11 1'] ['cpp','h'] 'utf-8' 500

right way:

python re-encoder.py "['1', '1 1', '11 1']" "['cpp','h']" 'utf-8' 500

to call the script in cmd

Passing parameters as strings will work. Why not do that?

import re
import sys
x = sys.argv[1:]
args = re.match(u"(\['[^']+'(?:,\s*'[^']+'){0,}\]) (\['[^']+'(?:,\s*'[^']+'){0,}\]) ('[^']+') ([1-9][0-9]*)",
                ' '.join(x))

g = args.groups()
print(g[0])
print(g[1])
print(g[2])
print(g[3])

try this.

$ python re-encoder.py "['1', '11', '111'] ['cpp', 'h'] 'utf-8' 500"

result

['1', '11', '111']
['cpp', 'h']
'utf-8'
500

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