需要一些正则表达帮助 [英] need some regular expression help

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

问题描述

我需要一个匹配字符串的模式,该字符串具有相同数量的''(''

as'')'':

findall(compile(' '...''),''42 ^((2x + 2)sin(x))+(log(2)/ log(5))'')= [

'' ((2x + 2)sin(x))'',''(log(2)/ log(5))'']

有人可以帮帮我吗?


感谢您的帮助!

I need a pattern that matches a string that has the same number of ''(''
as '')'':
findall( compile(''...''), ''42^((2x+2)sin(x)) + (log(2)/log(5))'' ) = [
''((2x+2)sin(x))'', ''(log(2)/log(5))'' ]
Can anybody help me out?

Thanks for any help!

推荐答案



Chris写道:

Chris wrote:

我需要一个匹配字符串的模式,该字符串具有相同数量的''(''

as'')'':

findall(compile('''''),''42 ^((2x + 2)sin(x))+(log(2)/ log(5))'')= [

''((2x + 2)sin(x))'',''(log(2)/ log(5))'']

任何人都可以帮帮我?
I need a pattern that matches a string that has the same number of ''(''
as '')'':
findall( compile(''...''), ''42^((2x+2)sin(x)) + (log(2)/log(5))'' ) = [
''((2x+2)sin(x))'', ''(log(2)/log(5))'' ]
Can anybody help me out?



这对于正则表达式是不可能的 - 他们不能记住

他们已经遇到过多少个parens。 />

你需要一个真正的解析器 - pyparsing似乎是今天最受欢迎的选择,我个人喜欢spark。我相信你会找到一个

示例语法,它将解析简单的算术表达式,比如上面的




Diez

This is not possible with regular expressions - they can''t "remember"
how many parens they already encountered.

You will need a real parser for this - pyparsing seems to be the most
popular choice today, I personally like spark. I''m sure you find an
example-grammar that will parse simple arithmetical expressions like
the one above.

Diez


Chris写道:
Chris wrote:

我需要一个匹配字符串的模式相同数量的''(''

as'')'':

findall(compile('''''),''42 ^(( 2x + 2)sin(x))+(log(2)/ log(5))'')= [

''((2x + 2)sin(x))'', ''(log(2)/ log(5))'']

有人可以帮帮我吗?
I need a pattern that matches a string that has the same number of ''(''
as '')'':
findall( compile(''...''), ''42^((2x+2)sin(x)) + (log(2)/log(5))'' ) = [
''((2x+2)sin(x))'', ''(log(2)/log(5))'' ]
Can anybody help me out?



不,有所以这样的模式。你必须编写一个函数。


考虑一下你的规范是什么:'42 ^((2x + 2)sin(x))+

(log(2)/ log(5))''具有相同数量的左括号和右括号;所以

执行零长度字符串;所以'')+('' - 也许你需要添加

''并以&开头('''


考虑你将如何处理这样的输入:


print''(''+ some_text +'')''


也许你需要做一些词法分析,并且工作在

代币而不是单个角色。


然后提出了一个常见的问题:你有一个感知那个

正则表达式是解决方案 - 什么问题?


HTH,

John

No, there is so such pattern. You will have to code up a function.

Consider what your spec really is: ''42^((2x+2)sin(x)) +
(log(2)/log(5))'' has the same number of left and right parentheses; so
does the zero-length string; so does '') + ('' -- perhaps you need to add
''and starts with a "("''

Consider what you are going to do with input like this:

print ''('' + some_text + '')''

Maybe you need to do some lexical analysis and work at the level of
tokens rather than individual characters.

Which then raises the usual question: you have a perception that
regular expressions are the solution -- to what problem??

HTH,
John


2006年10月7日15:00:29 -0700,Diez B. Roggisch< de *** @ web.dewrote:
On 7 Oct 2006 15:00:29 -0700, Diez B. Roggisch <de***@web.dewrote:

>

Chris写道:
>
Chris wrote:

我需要一个匹配具有相同数量'的字符串的模式'(''

as'')'':

findall(编译('''''),''42 ^((2x + 2)的sin(x)) +(log(2)/ log(5))'')= [

''((2x + 2)sin(x))'',''(log(2)/ log (5))'']

有人可以帮帮我吗?
I need a pattern that matches a string that has the same number of ''(''
as '')'':
findall( compile(''...''), ''42^((2x+2)sin(x)) + (log(2)/log(5))'' ) = [
''((2x+2)sin(x))'', ''(log(2)/log(5))'' ]
Can anybody help me out?



这对于正则表达式是不可能的 - 他们不能记住

他们已经遇到过多少个parens。


This is not possible with regular expressions - they can''t "remember"
how many parens they already encountered.



请记住,正则表达式用于表示常规的

语法。大多数正则表达式引擎实际上都不是常规的,因为它们支持像后视/前方和捕获组等花哨的东西...... IIRC,

这些不属于一个真正的正则表达式库。


据说,Lua中的quote-unquote regex有一个特殊的

功能,支持平衡表达式。我相信Python在某个地方有一个
PCRE lib;在这种情况下你可以使用实验性的?? {}

构造。


- Theerasak

Remember that regular expressions are used to represent regular
grammars. Most regex engines actually aren''t regular in that they
support fancy things like look-behind/ahead and capture groups...IIRC,
these cannot be part of a true regular expression library.

With that said, the quote-unquote regexes in Lua have a special
feature that supports balanced expressions. I believe Python has a
PCRE lib somewhere; you may be able to use the experimental ??{ }
construct in that case.

-- Theerasak


这篇关于需要一些正则表达帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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