Regex Match all characters between two strings

后端 未结 14 1042
星月不相逢
星月不相逢 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:48

    use this: (?<=beginningstringname)(.*\n?)(?=endstringname)

    0 讨论(0)
  • 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)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:51

    You can simply use this: \This is .*? \sentence

    0 讨论(0)
  • 2020-11-21 07:52

    Sublime Text 3x

    In sublime text, you simply write the two word you are interested in keeping for example in your case it is

    "This is" and "sentence"

    and you write .* in between

    i.e. This is .* sentence

    and this should do you well

    0 讨论(0)
  • 2020-11-21 07:53

    RegEx to match everything between two strings using the Java approach.

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

    Let's use Pattern and Matcher objects to use RegEx (.?)*.

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

    Since Matcher might contain more than one match, we need to loop over the results and store it.

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

    This example will contain only "will save the" word, but in the bigger text it will probably find more matches.

    0 讨论(0)
  • 2020-11-21 07:59

    for a quick search in VIM, you could use at Vim Control prompt: /This is.*\_.*sentence

    0 讨论(0)
提交回复
热议问题