Regex Match all characters between two strings

后端 未结 14 1047
星月不相逢
星月不相逢 2020-11-21 07:42

Example: \"This is just\\na simple sentence\".

I want to match every character between \"This is\" and \"sentence\". Line breaks should be ignored. I can\'t figure o

14条回答
  •  灰色年华
    2020-11-21 07:51

    In case anyone is looking for an example of this within a Jenkins context. It parses the build.log and if it finds a match it fails the build with the match.

    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)
            }
        }
    }
    

提交回复
热议问题