简体   繁体   中英

How to excute an external regular expression script via Python?

Or Should I change my question to 'how to implement regular expression tester like these': regular expression editor python

Maybe this is more like a programming question. My application will read in an article and expose a variable, say TEXT, to represent this article. Allow users to write Python re compatible script to manipulate the TEXT (the article), particularly to replace something.

For example, users could write the following commands, and my app should read it in and execute it:

p = re.compile( 'red')
p.sub( 'color', TEXT)

And I will release my app with something like py2exe, so I think How can I make one python file run another? doesn't work.

I know how to write regular expression, but now "using a Python script to run a Python script" really confuses me.

Do you want to write second word? You need to use input():

word = input('Specify color\n')

U can use just string.replace():

text = 'red'
text.replace('color', word)

If you want to use RegExp:

import re
re.sub('color', word, text)

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