例子: 这只是一个简单的句子。

我想匹配这句话和句子之间的每个字符。换行符应该被忽略。我想不出正确的语法。


当前回答

RegEx使用Java方法匹配两个字符串之间的所有内容。

List<String> results = new ArrayList<>(); //For storing results
String example = "Code will save the world";

让我们使用Pattern和Matcher对象来使用RegEx(.?)*。

Pattern p = Pattern.compile("Code "(.*?)" world");   //java.util.regex.Pattern;
Matcher m = p.matcher(example);                      //java.util.regex.Matcher;

由于Matcher可能包含多个匹配项,我们需要遍历结果并存储它。

while(m.find()){   //Loop through all matches
   results.add(m.group()); //Get value and store in collection.
}

这个例子将只包含“will save the”这个单词,但是在更大的文本中,它可能会找到更多匹配项。

其他回答

这适用于我(我使用VS Code):

: 这只是一个简单的句子

使用: +句子

如果有人在Jenkins上下文中寻找这样的例子。它会解析build.log,如果找到匹配,就会失败。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

node{    
    stage("parse"){
        def file = readFile 'build.log'

        def regex = ~"(?s)(firstStringToUse(.*)secondStringToUse)"
        Matcher match = regex.matcher(file)
        match.find() {
            capturedText = match.group(1)
            error(capturedText)
        }
    }
}

你可以简单地使用这个:\ this is .*?\句子

Sublime Text 3x

在崇高的文本中,你只需写下你感兴趣的两个单词,例如在你的例子中,它是

"This is"和"sentence"

在中间写。*

这是*句

这对你有好处

有一种方法来处理在文本块中这种分裂的重复实例?例如:“这只是一个简单的句子。这里还有一些额外的东西。这只是一个简单的句子。这里还有一些东西。这只是一个简单的句子。”。要匹配每个实例而不是整个字符串,使用下面的代码:

data = "This is just\na simple sentence. Here is some additional stuff. This is just\na simple sentence. And here is some more stuff. This is just\na simple sentence."

pattern = re.compile('This is (?s).*? sentence')

for match_instance in re.finditer(pattern, data):
    do_something(match_instance.group())