简体   繁体   English

Java 正则表达式前缀后缀

[英]Java Regex Prefix Suffix

I am finding it a bit difficult to wrap my head around prefix and suffix lookups in regular expressions.我发现在正则表达式中查找前缀和后缀有点困难。 I am practicing and want to do the following:我正在练习并想做以下事情:

Given a string: "James is good".给定一个字符串:“詹姆斯很好”。 I want to be able to match the maximal substring in-order, ie get a match if the text is "James"or "James is" or "James is good".我希望能够按顺序匹配最大 substring,即如果文本是“James”或“James is”或“James is good”,则得到匹配。 So if I have the following text: "James James is James", I should be able to capture "James is" and not just "James".因此,如果我有以下文本:“James James is James”,我应该能够捕捉到“James is”而不仅仅是“James”。 Simalrly " is James James is Good James" should give me "James is Good" and not "is James" as it is out of order and not maximal Simalrly “is James James is Good James”应该给我“James is Good”而不是“is James”,因为它是无序的而不是最大的

I think i can use suffix is not present(?,), to match, say only "James" if "is good" is not present and so on.我想我可以使用后缀不存在(?,)来匹配,如果“好”不存在则只说“詹姆斯”等等。 but I am not sure if I understand the concept of prefix and suffix matching correctly.但我不确定我是否正确理解前缀和后缀匹配的概念。

Any clarification or help in this case would be great.在这种情况下,任何澄清或帮助都会很棒。 I tagged java because I am familiarizing myself with java's regex api.我标记了 java 因为我熟悉 java 的正则表达式 api。

I think you mean that you want to capture "James is" and the next word if exists.我认为您的意思是要捕获“詹姆斯是”以及下一个单词(如果存在)。 In this case you should say "(James is(?:\s+\w+)?)" .在这种情况下,您应该说"(James is(?:\s+\w+)?)" Obviously in java code the back slashes must be duplicated.显然,在 java 代码中,反斜杠必须重复。

I have not run this regex but I believe it can give a good start to debug yours.我没有运行这个正则表达式,但我相信它可以为调试你的正则表达式提供一个良好的开端。

I am not sure, but I assume you are talking about look behind and look ahead assertions.我不确定,但我假设您正在谈论向后看和向前看的断言。

An assertion has zero length and is matching the empty string, so no characters are matched by this construct.断言的长度为零并且匹配空字符串,因此此构造不匹配任何字符。 You can use them to match a pattern only if it is preceded or followed by a certain other pattern (or not if you use the negative versions)只有在某个模式之前或之后才可以使用它们来匹配某个模式(或者,如果您使用否定版本,则不能)

You can check here for more details on regular-expressions.info您可以在此处查看有关regular-expressions.info的更多详细信息

The Perlretut is about Perl, but it works similarly in Java.Perlretut大约是 Perl,但它在 Java 中的工作原理类似。

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

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