RegExp问题 [英] RegExp question

查看:49
本文介绍了RegExp问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想形成一个正则表达式来查找几个不同的

令牌(和,或者,xor)后跟一些变量空格数

(即制表符和空格)后跟哈希标记(即#)。什么会这个正常的表达式?


感谢您的帮助,


Michael

解决方案

>我想,以形成一个正则表达式来找到几个后面紧跟着<空白(即,制表符和空格)的一些可变
不同的令牌(与,或,异或) br />哈希标记(即#)。什么是常规
表达式?



(和|或| xor)\ * *#


除非可变数量的空白意思是至少*一些*

空白,在这种情况下你会想要使用


(和|或| xor)\ s +#


两者都很精美。


-tim



Tim,


由于某种原因似乎没有成功。


我正在测试它grep的。 (即grep -e''(和|或| xor)\ s *#''myfile)


Michael

< br>

" Michael McGarry" < MI ************* @ gmail.com>在消息中写道

news:11 ********************** @ t31g2000cwb.googlegr oups.com ...



我想形成一个正则表达式来查找几个不同的令牌(和,或者,xor)后跟一些可变数量的空格
(即标签和空格)后跟哈希标记(即#)。什么会成为正则表达式?

感谢您的帮助,

Michael



使用pyparsing,空格被隐含地忽略了。你的表达式将是

看起来像:


oneOf("和xor")+ Literal(&#;")

Here''sa完整的例子:

。从pyparsing进口*


图案= oneOf(QUOT;和或XOR")+文字(QUOT;# ")


testString ="""

z =(a和b)和#XVAL;

q = z xor #YVAL;

"""

#使用scanString查找匹配项

代币,开始,结束模式。 scanString(的TestString):

打印令牌[0],tokens.asList()

打印线(启动,的TestString)

打印(QUOT ;" *(col(start,testString)-1))+" ^"

打印

打印

#use transformString找到匹配和替换值

subs = {

''XVAL'':0,

''YVAL'':是的,< br $>
}

def replaceSubs(st,loc,toks):

试试:

返回toks [0] + " " + str(subs [toks [2]])

除了KeyError:

pass


pattern2 =(pattern + Word( alphanums))。setParseAction(replaceSubs)

print pattern2.transformString(testString)


--------------- -

打印:

和[''和'',''#'']

z =(a和b)和# XVAL;

^


xor [''xor'',''#'']

q = z xor #YVAL ;

^

z =(a和b)和0;

q = z xor True;

下载pyparsing在 http://pyparsing.sourceforge.net


- 保罗


Hi,

I would like to form a regular expression to find a few different
tokens (and, or, xor) followed by some variable number of whitespace
(i.e., tabs and spaces) followed by a hash mark (i.e., #). What would
be the regular expression for this?

Thanks for any help,

Michael

解决方案

> I would like to form a regular expression to find a few

different tokens (and, or, xor) followed by some variable
number of whitespace (i.e., tabs and spaces) followed by
a hash mark (i.e., #). What would be the regular
expression for this?


(and|or|xor)\s*#

Unless "varible number of whitespace" means "at least *some*
whitespace", in which case you''d want to use

(and|or|xor)\s+#

Both are beautiful and precise.

-tim



Tim,

for some reason that does not seem to do the trick.

I am testing it with grep. (i.e., grep -e ''(and|or|xor)\s*#'' myfile)

Michael


"Michael McGarry" <mi*************@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...

Hi,

I would like to form a regular expression to find a few different
tokens (and, or, xor) followed by some variable number of whitespace
(i.e., tabs and spaces) followed by a hash mark (i.e., #). What would
be the regular expression for this?

Thanks for any help,

Michael


Using pyparsing, whitespace is implicitly ignored. Your expression would
look like:

oneOf("and or xor") + Literal("#")
Here''s a complete example:
from pyparsing import *

pattern = oneOf("and or xor") + Literal("#")

testString = """
z = (a and b) and #XVAL;
q = z xor #YVAL;
"""
# use scanString to locate matches
for tokens,start,end in pattern.scanString(testString):
print tokens[0], tokens.asList()
print line(start,testString)
print (" "*(col(start,testString)-1)) + "^"
print
print
# use transformString to locate matches and substitute values
subs = {
''XVAL'': 0,
''YVAL'': True,
}
def replaceSubs(st,loc,toks):
try:
return toks[0] + " " + str(subs[toks[2]])
except KeyError:
pass

pattern2 = (pattern + Word(alphanums)).setParseAction(replaceSubs)
print pattern2.transformString(testString)

-----------------
Prints:
and [''and'', ''#'']
z = (a and b) and #XVAL;
^

xor [''xor'', ''#'']
q = z xor #YVAL;
^
z = (a and b) and 0;
q = z xor True;
Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul


这篇关于RegExp问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆