Regex Match all characters between two strings

Regex

Regex Problem Overview


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 out the correct syntax.

Regex Solutions


Solution 1 - Regex

For example

(?<=This is)(.*)(?=sentence)

Regexr

I used lookbehind (?<=) and look ahead (?=) so that "This is" and "sentence" is not included in the match, but this is up to your use case, you can also simply write This is(.*)sentence.

The important thing here is that you activate the "dotall" mode of your regex engine, so that the . is matching the newline. But how you do this depends on your regex engine.

The next thing is if you use .* or .*?. The first one is greedy and will match till the last "sentence" in your string, the second one is lazy and will match till the next "sentence" in your string.

Update

Regexr

This is(?s)(.*)sentence

Where the (?s) turns on the dotall modifier, making the . matching the newline characters.

Update 2:

(?<=is \()(.*?)(?=\s*\))

is matching your example "This is (a simple) sentence". See here on Regexr

Solution 2 - Regex

Lazy Quantifier Needed

Resurrecting this question because the regex in the accepted answer doesn't seem quite correct to me. Why? Because

(?<=This is)(.*)(?=sentence)

will match my first sentence. This is my second in This is my first sentence. This is my second sentence.

[See demo][1].

You need a lazy quantifier between the two lookarounds. Adding a ? makes the star lazy.

This matches what you want:

(?<=This is).*?(?=sentence)

[See demo][2]. I removed the capture group, which was not needed.

DOTALL Mode to Match Across Line Breaks

Note that in the demo the "dot matches line breaks mode" (a.k.a.) dot-all is set (see how to turn on DOTALL in various languages). In many regex flavors, you can set it with the online modifier (?s), turning the expression into:

(?s)(?<=This is).*?(?=sentence)

Reference

Solution 3 - Regex

Try This is[\s\S]*?sentence, works in javascript

Solution 4 - Regex

This:

This is (.*?) sentence

works in javascript.

Solution 5 - Regex

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

Solution 6 - Regex

This worked for me (I'm using VS Code):

for: This is just\na simple sentence

Use: This .+ sentence

Solution 7 - Regex

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

Solution 8 - Regex

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

Solution 9 - Regex

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.

Solution 10 - Regex

I landed here on my search for regex to convert this print syntax between print "string", in Python2 in old scripts with: print("string"), for Python3. Works well, otherwise use 2to3.py for additional conversions. Here is my solution for others:

Try it out on Regexr.com (doesn't work in NP++ for some reason):

find:     (?<=print)( ')(.*)(')
replace: ('$2')

for variables:

(?<=print)( )(.*)(\n)
('$2')\n

for label and variable:

(?<=print)( ')(.*)(',)(.*)(\n)
('$2',$4)\n

https://stackoverflow.com/questions/53917898/how-to-replace-all-print-string-in-python2-with-printstring-for-python3/53917912#53917912

Solution 11 - Regex

There is a way to deal with repeated instances of this split in a block of text? FOr instance: "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. ". to matches each instance instead of the entire string, use below code:

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

Solution 12 - Regex

Here is how I did it:
This was easier for me than trying to figure out the specific regex necessary.

int indexPictureData = result.IndexOf("-PictureData:");
int indexIdentity = result.IndexOf("-Identity:");
string returnValue = result.Remove(indexPictureData + 13);
returnValue = returnValue + " [bytecoderemoved] " + result.Remove(0, indexIdentity); ` 

Solution 13 - Regex

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

Solution 14 - Regex

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question0xbadf00dView Question on Stackoverflow
Solution 1 - RegexstemaView Answer on Stackoverflow
Solution 2 - Regexzx81View Answer on Stackoverflow
Solution 3 - RegexkaoreView Answer on Stackoverflow
Solution 4 - RegexRiyafa Abdul HameedView Answer on Stackoverflow
Solution 5 - RegexvigneshView Answer on Stackoverflow
Solution 6 - RegexRoshna OmerView Answer on Stackoverflow
Solution 7 - RegexAnirbanDebnathView Answer on Stackoverflow
Solution 8 - RegexCephosView Answer on Stackoverflow
Solution 9 - RegexAlexander GolovinovView Answer on Stackoverflow
Solution 10 - RegexalchemyView Answer on Stackoverflow
Solution 11 - RegexYahya HassaniView Answer on Stackoverflow
Solution 12 - RegexBbbView Answer on Stackoverflow
Solution 13 - RegexvinsView Answer on Stackoverflow
Solution 14 - RegexAnonymousView Answer on Stackoverflow