元素,我想从一个包含字符串中的表达式元素。C#正则表达式进去表达
例如:获得从表达元件,其中表达式是
@@Expr[propAge>10?'ignore>':'ignore?']
和表达式是
@@Expr[PropSex='M'?'ignore>']"
在这种情况下,我感兴趣的由上述获得两个字符串数组串
array1 = ["propAge",">","'ignore>'","'ignore?'"]
array2 = ["propSex","=","'M'","'ignore>'"]
元素,我想从一个包含字符串中的表达式元素。C#正则表达式进去表达
例如:获得从表达元件,其中表达式是
@@Expr[propAge>10?'ignore>':'ignore?']
和表达式是
@@Expr[PropSex='M'?'ignore>']"
在这种情况下,我感兴趣的由上述获得两个字符串数组串
array1 = ["propAge",">","'ignore>'","'ignore?'"]
array2 = ["propSex","=","'M'","'ignore>'"]
通常写作只用正则表达式解析器是一个坏主意,因为你试图区分在不同环境下相似的令牌时遇到各种有趣的问题。而且,只有两个样本往往不足以写出一个好的正则表达式。
话虽这么说,你的情况,你也许可以使用类似以下内容:
(?xi) # turn on comments mode and case-insensitivity
'[^']+' # strings like 'ignore?'
|
(?<=\[.*) # ensure that there's an opening square bracket to the left
# to not match the @@Expr part
[a-z]+ # matches e.g. propAge
|
[<>=] # operators
它使用了大量的交替来表达的不同部分匹配。
我不能完全肯定,但我认为这个问题完全被破坏的编辑。我认为*“例如为:” *后的文本最初被认为是为了得到结果要分析的源字符串? – grek40
你应该**首先尝试**的东西。 – tchelidze