示例:「這只是一個簡單的句子」。 javascript
我想匹配「這是」和「句子」之間的每一個字符。 換行符應忽略。 我找不到正確的語法。 html
使用這個: (?<=beginningstringname)(.*\\n?)(?=endstringname)
java
從新提出這個問題,由於接受的答案中的正則表達式對我而言彷佛不太正確。 爲何? 由於 node
(?<=This is)(.*)(?=sentence)
會匹配my first sentence. This is my second
my first sentence. This is my second
This is my first sentence. This is my second sentence.
This is my first sentence. This is my second sentence.
正則表達式
參見演示 。 express
您須要在兩種環視之間使用惰性的量詞。 添加?
使星星懶。 ui
這符合您的要求: spa
(?<=This is).*?(?=sentence)
參見演示 。 我刪除了捕獲組,這不是必需的。 code
DOTALL模式以匹配整個換行符 htm
請注意,在演示中,設置了「點匹配換行模式」(aka)點全名(請參閱如何以各類語言打開DOTALL )。 在許多正則表達式中,您能夠使用在線修飾符(?s)
,將表達式轉換爲:
(?s)(?<=This is).*?(?=sentence)
參考
這個:
This is (.*?) sentence
在javascript中工做。
您能夠簡單地使用: \\This is .*? \\sentence
\\This is .*? \\sentence
若是有人在詹金斯語境中尋找這樣的例子。 它解析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) } } }