简体   繁体   English

如何通过Python执行外部正则表达式脚本?

[英]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 还是我应该将问题更改为“如何实现这样的正则表达式测试器”: 正则表达式编辑器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. 我的应用程序将阅读一篇文章,并公开一个变量(例如TEXT)来表示本文。 Allow users to write Python re compatible script to manipulate the TEXT (the article), particularly to replace something. 允许用户编写与Python兼容的脚本来操纵TEXT(本文),尤其是替换某些东西。

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? 而且我将使用py2exe之类的东西发布我的应用程序,所以我认为如何使一个python文件运行另一个? doesn't work. 不起作用。

I know how to write regular expression, but now "using a Python script to run a Python script" really confuses me. 我知道如何编写正则表达式,但是现在“使用Python脚本运行Python脚本”确实使我感到困惑。

Do you want to write second word? 你想写第二个单词吗? You need to use input(): 您需要使用input():

word = input('Specify color\n')

U can use just string.replace(): 您可以只使用string.replace():

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

If you want to use RegExp: 如果要使用RegExp:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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