java 理解正则表达式 if then 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12691404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
understanding regex if then statements
提问by Travis Crum
So I'm not sure if I understand how this works and would like
a simple explanation to how they work
is all. I probably have it way off. A pure regex solution is required, and I don't know if this is possible
. If it is, a solution would be awesome too, but a shove in the right direction would be good for my learning process ^_^
所以我不确定我是否理解它是如何工作的,并且想要的
a simple explanation to how they work
就是全部。我可能有它的方式。A pure regex solution is required, and I don't know if this is possible
. 如果是这样,解决方案也很棒,但是朝正确的方向推动对我的学习过程有好处^_^
This is how I thought the if/then/else
option built into my regex engines was formatted:
这就是我认为if/then/else
我的正则表达式引擎中内置的选项的格式:
?(condition)if regex|else regex
I want it to capture a string from a very specific location only when this string exists within a certain section of javascript. Because this is how I thought it worked after a decent amount of research I tried out a few variations of this code but they all ended up something like this.
我希望它仅在此字符串存在于 javascript 的某个部分时才从非常特定的位置捕获字符串。因为这是经过大量研究后我认为它是如何工作的,所以我尝试了这段代码的一些变体,但它们最终都变成了这样。
((?^view_large$)Tables-137(.*?)search.htm)
Also of relevance: I'm using an java based app that has regex searches which pull the data I need so I cannot write an if statement in java which would be my preferred method.
It's a pain to have to do it this way, but at the moment I have no other choice. I'm trying really hard for them to allow java code functionality instead of pure regex for more versatile options.
Also of relevance: I'm using an java based app that has regex searches which pull the data I need so I cannot write an if statement in java which would be my preferred method.
不得不这样做很痛苦,但目前我别无选择。我真的很努力地让他们允许 Java 代码功能而不是纯正则表达式以获得更多功能的选项。
So to summarize, is there even a if/then option in regex and if so how is it formatted for what I'm trying to accomplish?
所以总结一下, is there even a if/then option in regex and if so how is it formatted for what I'm trying to accomplish?
EDIT: The string that I want to be the "if condition" is like this: if view_large
string exists and is not null then capture the exact string 500/
which is captured within the catch all group I used: (.*?)
编辑:我想成为“if 条件”的view_large
字符串是这样的:如果字符串存在且不为空,则捕获在500/
我使用的 catch all 组中捕获的确切字符串:(.*?)
回答by dasblinkenlight
There is no conditionals in Java regexp, but you can simulate them by writing two expressions that include mutually exclusive look-behind constructs, like this:
Java regexp 中没有条件,但您可以通过编写两个包含互斥后视结构的表达式来模拟它们,如下所示:
((?<=if )then)|((?<!if )end)
This expression will match "then" when it is preceded by an "if "
; it will match "end"
when it is notpreceded by an "if "
当它前面有一个"if "
;时,这个表达式将匹配“then” 。"end"
当它前面没有一个时,它将匹配"if "
回答by ruakh
The Javadoc for java.util.regex.Pattern
mentions, in its list of "Perl constructs not supported by this class":
用于java.util.regex.Pattern
提及的 Javadoc,在其“此类不支持的 Perl 构造”列表中:
- The conditional constructs
(?(condition)X)
and(?(condition)X|Y)
.
- 条件构造和。
(?(condition)X)
(?(condition)X|Y)
So, no dice. But you should look through the Javadoc to see if you can achieve what you need by using regex features that it doessupport. (Or, if you post some more detailed examples, we can try to help.)
所以,没有骰子。但是,你应该看看通过的Javadoc,看看你能达到你需要使用正则表达式的功能,它是什么做支持。(或者,如果您发布一些更详细的示例,我们可以尝试提供帮助。)
回答by Andrew Cheong
Try lookaround assertions.
尝试环视断言。
For example, say you want to capture FOOBAR only if there is a 4+ digit number somewhere:
例如,假设您只想在某处有 4+ 位数字时捕获 FOOBAR:
(?=.*\d{4}).*(FOOBAR)