How do I match any character across multiple lines in a regular expression?












264















For example, this regex



(.*)<FooBar>


will match:



abcde<FooBar>


But how do I get it to match across multiple lines?



abcde
fghij<FooBar>









share|improve this question




















  • 1





    To clarify; I was originally using Eclipse to do a find and replace in multiple files. What I have discovered by the answers below is that my problem was the tool and not regex pattern.

    – andyuk
    Oct 2 '08 at 15:45






  • 2





    Your flag "eclipse" should be removed then because one looking for an eclipse solution will find this question (like I did) and then find a non-eclipse solution as accepted one.

    – acme
    Jun 13 '12 at 12:09











  • Now I'm finding this in the search engine because eclipse was mentioned. Oh the horror.

    – Brian Olsen
    Mar 20 '18 at 19:29
















264















For example, this regex



(.*)<FooBar>


will match:



abcde<FooBar>


But how do I get it to match across multiple lines?



abcde
fghij<FooBar>









share|improve this question




















  • 1





    To clarify; I was originally using Eclipse to do a find and replace in multiple files. What I have discovered by the answers below is that my problem was the tool and not regex pattern.

    – andyuk
    Oct 2 '08 at 15:45






  • 2





    Your flag "eclipse" should be removed then because one looking for an eclipse solution will find this question (like I did) and then find a non-eclipse solution as accepted one.

    – acme
    Jun 13 '12 at 12:09











  • Now I'm finding this in the search engine because eclipse was mentioned. Oh the horror.

    – Brian Olsen
    Mar 20 '18 at 19:29














264












264








264


62






For example, this regex



(.*)<FooBar>


will match:



abcde<FooBar>


But how do I get it to match across multiple lines?



abcde
fghij<FooBar>









share|improve this question
















For example, this regex



(.*)<FooBar>


will match:



abcde<FooBar>


But how do I get it to match across multiple lines?



abcde
fghij<FooBar>






regex multiline






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 19 '15 at 20:59









Bobulous

9,81642850




9,81642850










asked Oct 1 '08 at 18:48









andyukandyuk

23.5k154451




23.5k154451








  • 1





    To clarify; I was originally using Eclipse to do a find and replace in multiple files. What I have discovered by the answers below is that my problem was the tool and not regex pattern.

    – andyuk
    Oct 2 '08 at 15:45






  • 2





    Your flag "eclipse" should be removed then because one looking for an eclipse solution will find this question (like I did) and then find a non-eclipse solution as accepted one.

    – acme
    Jun 13 '12 at 12:09











  • Now I'm finding this in the search engine because eclipse was mentioned. Oh the horror.

    – Brian Olsen
    Mar 20 '18 at 19:29














  • 1





    To clarify; I was originally using Eclipse to do a find and replace in multiple files. What I have discovered by the answers below is that my problem was the tool and not regex pattern.

    – andyuk
    Oct 2 '08 at 15:45






  • 2





    Your flag "eclipse" should be removed then because one looking for an eclipse solution will find this question (like I did) and then find a non-eclipse solution as accepted one.

    – acme
    Jun 13 '12 at 12:09











  • Now I'm finding this in the search engine because eclipse was mentioned. Oh the horror.

    – Brian Olsen
    Mar 20 '18 at 19:29








1




1





To clarify; I was originally using Eclipse to do a find and replace in multiple files. What I have discovered by the answers below is that my problem was the tool and not regex pattern.

– andyuk
Oct 2 '08 at 15:45





To clarify; I was originally using Eclipse to do a find and replace in multiple files. What I have discovered by the answers below is that my problem was the tool and not regex pattern.

– andyuk
Oct 2 '08 at 15:45




2




2





Your flag "eclipse" should be removed then because one looking for an eclipse solution will find this question (like I did) and then find a non-eclipse solution as accepted one.

– acme
Jun 13 '12 at 12:09





Your flag "eclipse" should be removed then because one looking for an eclipse solution will find this question (like I did) and then find a non-eclipse solution as accepted one.

– acme
Jun 13 '12 at 12:09













Now I'm finding this in the search engine because eclipse was mentioned. Oh the horror.

– Brian Olsen
Mar 20 '18 at 19:29





Now I'm finding this in the search engine because eclipse was mentioned. Oh the horror.

– Brian Olsen
Mar 20 '18 at 19:29












21 Answers
21






active

oldest

votes


















193














It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:



/(.*)<FooBar>/s


The s at the end causes the dot to match all characters including newlines.






share|improve this answer



















  • 3





    @Grace: use n to match a newline

    – Jeremy Ruten
    Apr 11 '11 at 21:05






  • 5





    rn works perfectly

    – Grace
    Apr 12 '11 at 8:08






  • 2





    The s flag is (now?) invalid, at least in Chrome/V8. Instead use /([sS]*)<FooBar>/ character class (match space and non-space] instead of the period matcher. See other answers for more info.

    – Allen
    May 9 '13 at 15:37








  • 2





    @Allen - JavaScript doesn't support the s modifier. Instead, do [^]* for the same effect.

    – Derek 朕會功夫
    Jul 12 '15 at 22:26






  • 1





    In Ruby, use the m modifier

    – Ryan Buckley
    Jul 15 '15 at 22:57



















264














Try this:



((.|n)*)<FooBar>


It basically says "any character or a newline" repeated zero or more times.






share|improve this answer



















  • 3





    This is dependent on the language and/or tool you are using. Please let us know what you are using, eg Perl, PHP, CF, C#, sed, awk, etc.

    – Ben Doom
    Oct 1 '08 at 18:57






  • 31





    Depending on your line endings you might need ((.|n|r)*)<FooBar>

    – Potherca
    Mar 9 '12 at 17:27






  • 3





    He said he is using Eclipse. This is correct solution in my opinion. I have same problem and this solved it.

    – Danubian Sailor
    Apr 18 '12 at 8:14






  • 4





    Right - the question is about eclipse and so are the tags. But the accepted solution is a PHP solution. Yours should be the accepted solution...

    – acme
    Jun 13 '12 at 12:04






  • 6





    This is the worst regex for matching multiple line input. Please never use it unless you are using ElasticSearch. Use [sS]* or (?s).*.

    – Wiktor Stribiżew
    Jul 18 '16 at 11:05





















61














If you're using Eclipse search, you can enable the "DOTALL" option to make '.' match any character including line delimiters: just add "(?s)" at the beginning of your search string. Example:



(?s).*<FooBar>





share|improve this answer



















  • 8





    This is not eclipse-specific, should work anywhere.

    – Steven Soroka
    Oct 8 '13 at 16:50











  • Not anywhere, only in regex flavors supporting inline modifiers, and certainly not in Ruby where (?s) => (?m)

    – Wiktor Stribiżew
    Jul 18 '16 at 11:06











  • Anything for bash?

    – Pasupathi Rajamanickam
    Dec 19 '18 at 2:12



















46














The question is, can . pattern match any character? The answer varies from engine to engine. The main difference is whether the pattern is used by a POSIX or non-POSIX regex library.



Special note about lua-patterns: they are not considered regular expressions, but . matches any char there, same as POSIX based engines.



Another note on matlab and octave: the . matches any char by default (demo): str = "abcden fghij<Foobar>"; expression = '(.*)<Foobar>*'; [tokens,matches] = regexp(str,expression,'tokens','match'); (tokens contain a abcden fghij item).



Also, in all of boost's regex grammars the dot matches line breaks by default. Boost's ECMAScript grammar allows you to turn this off with regex_constants::no_mod_m (source).



As for oracle (it is POSIX based), use n option (demo): select regexp_substr('abcde' || chr(10) ||' fghij<Foobar>', '(.*)<Foobar>', 1, 1, 'n', 1) as results from dual



POSIX-based engines:



A mere . already matches line breaks, no need to use any modifiers, see bash (demo).



The tcl (demo), postgresql (demo), r (TRE, base R default engine with no perl=TRUE, for base R with perl=TRUE or for stringr/stringi patterns, use the (?s) inline modifier) (demo) also treat . the same way.



However, most POSIX based tools process input line by line. Hence, . does not match the line breaks just because they are not in scope. Here are some examples how to override this:





  • sed - There are multiple workarounds, the most precise but not very safe is sed 'H;1h;$!d;x; s/(.*)><Foobar>/1/' (H;1h;$!d;x; slurps the file into memory). If whole lines must be included, sed '/start_pattern/,/end_pattern/d' file (removing from start will end with matched lines included) or sed '/start_pattern/,/end_pattern/{{//!d;};}' file (with matching lines excluded) can be considered.


  • perl - perl -0pe 's/(.*)<FooBar>/$1/gs' <<< "$str" (-0 slurps the whole file into memory, -p prints the file after applying the script given by -e). Note that using -000pe will slurp the file and activate 'paragraph mode' where Perl uses consecutive newlines (nn) as the record separator.


  • gnu-grep - grep -Poz '(?si)abcK.*?(?=<Foobar>)' file. Here, z enables file slurping, (?s) enables the DOTALL mode for the . pattern, (?i) enables case insensitive mode, K omits the text matched so far, *? is a lazy quantifier, (?=<Foobar>) matches the location before <Foobar>.


  • pcregrep - pcregrep -Mi "(?si)abcK.*?(?=<Foobar>)" file (M enables file slurping here). Note pcregrep is a good solution for Mac OS grep users.


See demos.



Non-POSIX-based engines:





  • php - Use s modifier PCRE_DOTALL modifier: preg_match('~(.*)<Foobar>~s', $s, $m) (demo)


  • c# - Use RegexOptions.Singleline flag (demo):
    - var result = Regex.Match(s, @"(.*)<Foobar>", RegexOptions.Singleline).Groups[1].Value;
    - var result = Regex.Match(s, @"(?s)(.*)<Foobar>").Groups[1].Value;


  • powershell - Use (?s) inline option: $s = "abcde`nfghij<FooBar>"; $s -match "(?s)(.*)<Foobar>"; $matches[1]


  • perl - Use s modifier (or (?s) inline version at the start) (demo): /(.*)<FooBar>/s


  • python - Use re.DOTALL (or re.S) flags or (?s) inline modifier (demo): m = re.search(r"(.*)<FooBar>", s, flags=re.S) (and then if m:, print(m.group(1)))


  • java - Use Pattern.DOTALL modifier (or inline (?s) flag) (demo): Pattern.compile("(.*)<FooBar>", Pattern.DOTALL)


  • groovy - Use (?s) in-pattern modifier (demo): regex = /(?s)(.*)<FooBar>/


  • scala - Use (?s) modifier (demo): "(?s)(.*)<Foobar>".r.findAllIn("abcden fghij<Foobar>").matchData foreach { m => println(m.group(1)) }


  • javascript - Use [^] or workarounds [dD] / [wW] / [sS] (demo): s.match(/([sS]*)<FooBar>/)[1]


  • c++ (std::regex) Use [sS] or the JS workarounds (demo): regex rex(R"(([sS]*)<FooBar>)");


  • vba - Use the same approach as in JavaScript, ([sS]*)<Foobar>.


  • ruby - Use /m MULTILINE modifier (demo): s[/(.*)<Foobar>/m, 1]


  • go - Use the inline modifier (?s) at the start (demo): re: = regexp.MustCompile(`(?s)(.*)<FooBar>`)


  • swift - Use dotMatchesLineSeparators or (easier) pass the (?s) inline modifier to the pattern: let rx = "(?s)(.*)<Foobar>"


  • objective-c - Same as Swift, (?s) works the easiest, but here is how the option can be used: NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern
    options:NSRegularExpressionDotMatchesLineSeparators error:&regexError];



  • re2, google-apps-script - Use (?s) modifier (demo): "(?s)(.*)<Foobar>" (in Google Spreadsheets, =REGEXEXTRACT(A2,"(?s)(.*)<Foobar>"))


NOTES ON (?s):



In most non-POSIX engines, (?s) inline modifier (or embedded flag option) can be used to enforce . to match line breaks.



If placed at the start of the pattern, (?s) changes the bahavior of all . in the pattern. If the (?s) is placed somewhere after the beginning, only those . will be affected that are located to the right of it unless this is a pattern passed to Python re. In Python re, regardless of the (?s) location, the whole pattern . are affected. The (?s) effect is stopped using (?-s). A modified group can be used to only affect a specified range of a regex pattern (e.g. Delim1(?s:.*?)nDelim2.* will make the first .*? match across newlines and the second .* will only match the rest of the line).



POSIX note:



In non-regex engines, to match any char, [sS] / [dD] / [wW] constructs can be used.



In POSIX, [sS] is not matching any char (as in JavaScript or any non-POSIX engine) because regex escape sequences are not supported inside bracket expressions. [sS] is parsed as bracket expressions that match a single char, or s or S.






share|improve this answer





















  • 4





    You should link to this excellent overview from your profile page or something (+1).

    – Jan
    Oct 15 '17 at 20:15






  • 1





    You may want to add this to the boost item: In the regex_constants namespace, flag_type_'s : perl = ECMAScript = JavaScript = JScript = ::boost::regbase::normal = 0 which defaults to Perl. Programmers will set a base flag definition #define MOD regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m for thier regex flags to reflect that. And the arbitor is always the inline modifiers. Where (?-sm)(?s).* resets.

    – sln
    Apr 26 '18 at 21:30








  • 1





    Can you also add for bash please?

    – Pasupathi Rajamanickam
    Dec 19 '18 at 2:12






  • 2





    @PasupathiRajamanickam Bash uses a POSIX regex engine, the . matches any char there (including line breaks). See this online Bash demo.

    – Wiktor Stribiżew
    Dec 19 '18 at 7:33





















31














In JavaScript, use /[Ss]*<Foobar>/. Source






share|improve this answer





















  • 2





    From that link: "JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [sS] to match any character." Instead of the . use [sS] (match spaces and non-spaces) instead.

    – Allen
    May 9 '13 at 15:34



















27














([sS]*)<FooBar>



The dot matches all except newlines (rn). So use sS, which will match ALL characters.






share|improve this answer
























  • This solve the problem if you are using the Objective-C [text rangeOfString:regEx options:NSRegularExpressionSearch]. Thanks!

    – J. Costa
    Aug 24 '12 at 22:29













  • This works in intelliJ's find&replace regex, thanks.

    – barclay
    Sep 16 '15 at 22:14











  • This works. But it needs to be the first occurrence of <FooBar>

    – Ozkan
    Sep 26 '17 at 14:16



















18














In Ruby ruby you can use the 'm' option (multiline):



/YOUR_REGEXP/m


See the Regexp documentation on ruby-doc.org for more information.






share|improve this answer

































    10














    we can also use



    (.*?n)*?


    to match everything including newline without greedy



    This will make the new line optional



    (.*?|n)*?





    share|improve this answer































      8














      "." normally doesn't match line-breaks. Most regex engines allows you to add the S-flag (also called DOTALL and SINGLELINE) to make "." also match newlines.
      If that fails, you could do something like [Ss].






      share|improve this answer

































        7














        For Eclipse worked following expression:




        Foo



        jadajada Bar"




        Regular-Expression:



        Foo[Ss]{1,10}.*Bar*





        share|improve this answer

































          5














          /(.*)<FooBar>/s


          the s causes Dot (.) to match carriage returns






          share|improve this answer
























          • Seems like this is invalid (Chrome): text.match(/a/s) SyntaxError: Invalid flags supplied to RegExp constructor 's'

            – Allen
            May 9 '13 at 15:31













          • Because it is unsupported in JavaScript RegEx engines. The s flags exists in PCRE, the most complete engine (available in Perl and PHP). PCRE has 10 flags (and a lot of other features) while JavaScript has only 3 flags (gmi).

            – Morgan Touverey Quilling
            Apr 20 '16 at 18:51



















          4














          In java based regular expression you can use [sS]






          share|improve this answer





















          • 1





            Shouldn't those be backslashes?

            – Paul Draper
            Oct 19 '13 at 6:48











          • They go at the end of the Regular Expression, not within in. Example: /blah/s

            – RandomInsano
            Dec 21 '13 at 20:12











          • I guess you mean JavaScript, not Java? Since you can just add the s flag to the pattern in Java and JavaScript doesn't have the s flag.

            – 3limin4t0r
            Sep 25 '18 at 17:47





















          3














          Note that (.|n)* can be less efficient than (for example) [sS]* (if your language's regexes support such escapes) and than finding how to specify the modifier that makes . also match newlines. Or you can go with POSIXy alternatives like [[:space:][:^space:]]*.






          share|improve this answer































            3














            Use RegexOptions.Singleline, it changes the meaning of . to include newlines



            Regex.Replace(content, searchText, replaceText, RegexOptions.Singleline);






            share|improve this answer































              2














              Solution:



              Use pattern modifier sU will get the desired matching in PHP.



              example:



              preg_match('/(.*)/sU',$content,$match);


              Source:



              http://dreamluverz.com/developers-tools/regex-match-all-including-new-line
              http://php.net/manual/en/reference.pcre.pattern.modifiers.php






              share|improve this answer































                1














                In the context of use within languages, regular expressions act on strings, not lines. So you should be able to use the regex normally, assuming that the input string has multiple lines.



                In this case, the given regex will match the entire string, since "<FooBar>" is present. Depending on the specifics of the regex implementation, the $1 value (obtained from the "(.*)") will either be "fghij" or "abcdenfghij". As others have said, some implementations allow you to control whether the "." will match the newline, giving you the choice.



                Line-based regular expression use is usually for command line things like egrep.






                share|improve this answer

































                  1














                  I had the same problem and solved it in probably not the best way but it works. I replaced all line breaks before I did my real match:



                  mystring= Regex.Replace(mystring, "rn", "")


                  I am manipulating HTML so line breaks don't really matter to me in this case.



                  I tried all of the suggestions above with no luck, I am using .Net 3.5 FYI






                  share|improve this answer
























                  • I am using .NET too and (s|S) seems to do the trick for me!

                    – Vamshi Krishna
                    May 18 '18 at 7:26













                  • @VamshiKrishna In .NET, use (?s) to make . match any chars. Do not use (s|S) that will slow down performance.

                    – Wiktor Stribiżew
                    Sep 14 '18 at 20:35



















                  0














                  generally . doesn't match newlines, so try ((.|n)*)<foobar>






                  share|improve this answer



















                  • 1





                    No, don't do that. If you need to match anything including line separators, use the DOTALL (a.k.a. /s or SingleLine) modifier. Not only does the (.|n) hack make the regex less efficient, it's not even correct. At the very least, it should match r (carriage return) as well as n (linefeed). There are other line separator characters, too, albeit rarely used. But if you use the DOTALL flag, you don't have to worry about them.

                    – Alan Moore
                    Apr 26 '09 at 3:17






                  • 1





                    R is the platform-independent match for newlines in Eclipse.

                    – opyate
                    Nov 30 '09 at 11:13











                  • @opyate You should post this as an answer as this little gem is incredibly useful.

                    – jeckhart
                    Oct 15 '12 at 21:29











                  • You could try this instead. It won't match the inner brackets and also consider the optionalr.: ((?:.|r?n)*)<foobar>

                    – ssc-hrep3
                    Nov 29 '16 at 9:52



















                  0














                  I wanted to match a particular if block in java



                     ...
                  ...
                  if(isTrue){
                  doAction();

                  }
                  ...
                  ...
                  }


                  If I use the regExp



                  if (isTrue(.|n)*}


                  it included the closing brace for the method block so I used



                  if (!isTrue([^}.]|n)*}


                  to exclude the closing brace from the wildcard match.






                  share|improve this answer































                    0














                    Often we have to modify a substring with a few keywords spread across lines preceding the substring. Consider an xml element:



                    <TASK>
                    <UID>21</UID>
                    <Name>Architectural design</Name>
                    <PercentComplete>81</PercentComplete>
                    </TASK>


                    Suppose we want to modify the 81, to some other value, say 40. First identify .UID.21..UID., then skip all characters including n till .PercentCompleted.. The regular expression pattern and the replace specification are:



                    String hw = new String("<TASK>n  <UID>21</UID>n  <Name>Architectural design</Name>n  <PercentComplete>81</PercentComplete>n</TASK>");
                    String pattern = new String ("(<UID>21</UID>)((.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                    String replaceSpec = new String ("$1$2$440$6");
                    //note that the group (<PercentComplete>) is $4 and the group ((.|n)*?) is $2.

                    String iw = hw.replaceFirst(pattern, replaceSpec);
                    System.out.println(iw);

                    <TASK>
                    <UID>21</UID>
                    <Name>Architectural design</Name>
                    <PercentComplete>40</PercentComplete>
                    </TASK>


                    The subgroup (.|n) is probably the missing group $3. If we make it non-capturing by (?:.|n) then the $3 is (<PercentComplete>). So the pattern and replaceSpec can also be:



                    pattern = new String("(<UID>21</UID>)((?:.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                    replaceSpec = new String("$1$2$340$5")


                    and the replacement works correctly as before.






                    share|improve this answer

































                      0














                      In Javascript you can use [^]* to search for zero to infinite characters, including line breaks.






                      $("#find_and_replace").click(function() {
                      var text = $("#textarea").val();
                      search_term = new RegExp("[^]*<Foobar>", "gi");;
                      replace_term = "Replacement term";
                      var new_text = text.replace(search_term, replace_term);
                      $("#textarea").val(new_text);
                      });

                      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                      <button id="find_and_replace">Find and replace</button>
                      <br>
                      <textarea ID="textarea">abcde
                      fghij&lt;Foobar&gt;</textarea>








                      share|improve this answer























                        Your Answer






                        StackExchange.ifUsing("editor", function () {
                        StackExchange.using("externalEditor", function () {
                        StackExchange.using("snippets", function () {
                        StackExchange.snippets.init();
                        });
                        });
                        }, "code-snippets");

                        StackExchange.ready(function() {
                        var channelOptions = {
                        tags: "".split(" "),
                        id: "1"
                        };
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function() {
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled) {
                        StackExchange.using("snippets", function() {
                        createEditor();
                        });
                        }
                        else {
                        createEditor();
                        }
                        });

                        function createEditor() {
                        StackExchange.prepareEditor({
                        heartbeatType: 'answer',
                        autoActivateHeartbeat: false,
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader: {
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        },
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        });


                        }
                        });














                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f159118%2fhow-do-i-match-any-character-across-multiple-lines-in-a-regular-expression%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown

























                        21 Answers
                        21






                        active

                        oldest

                        votes








                        21 Answers
                        21






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        193














                        It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:



                        /(.*)<FooBar>/s


                        The s at the end causes the dot to match all characters including newlines.






                        share|improve this answer



















                        • 3





                          @Grace: use n to match a newline

                          – Jeremy Ruten
                          Apr 11 '11 at 21:05






                        • 5





                          rn works perfectly

                          – Grace
                          Apr 12 '11 at 8:08






                        • 2





                          The s flag is (now?) invalid, at least in Chrome/V8. Instead use /([sS]*)<FooBar>/ character class (match space and non-space] instead of the period matcher. See other answers for more info.

                          – Allen
                          May 9 '13 at 15:37








                        • 2





                          @Allen - JavaScript doesn't support the s modifier. Instead, do [^]* for the same effect.

                          – Derek 朕會功夫
                          Jul 12 '15 at 22:26






                        • 1





                          In Ruby, use the m modifier

                          – Ryan Buckley
                          Jul 15 '15 at 22:57
















                        193














                        It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:



                        /(.*)<FooBar>/s


                        The s at the end causes the dot to match all characters including newlines.






                        share|improve this answer



















                        • 3





                          @Grace: use n to match a newline

                          – Jeremy Ruten
                          Apr 11 '11 at 21:05






                        • 5





                          rn works perfectly

                          – Grace
                          Apr 12 '11 at 8:08






                        • 2





                          The s flag is (now?) invalid, at least in Chrome/V8. Instead use /([sS]*)<FooBar>/ character class (match space and non-space] instead of the period matcher. See other answers for more info.

                          – Allen
                          May 9 '13 at 15:37








                        • 2





                          @Allen - JavaScript doesn't support the s modifier. Instead, do [^]* for the same effect.

                          – Derek 朕會功夫
                          Jul 12 '15 at 22:26






                        • 1





                          In Ruby, use the m modifier

                          – Ryan Buckley
                          Jul 15 '15 at 22:57














                        193












                        193








                        193







                        It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:



                        /(.*)<FooBar>/s


                        The s at the end causes the dot to match all characters including newlines.






                        share|improve this answer













                        It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:



                        /(.*)<FooBar>/s


                        The s at the end causes the dot to match all characters including newlines.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 1 '08 at 18:52









                        Jeremy RutenJeremy Ruten

                        125k34157184




                        125k34157184








                        • 3





                          @Grace: use n to match a newline

                          – Jeremy Ruten
                          Apr 11 '11 at 21:05






                        • 5





                          rn works perfectly

                          – Grace
                          Apr 12 '11 at 8:08






                        • 2





                          The s flag is (now?) invalid, at least in Chrome/V8. Instead use /([sS]*)<FooBar>/ character class (match space and non-space] instead of the period matcher. See other answers for more info.

                          – Allen
                          May 9 '13 at 15:37








                        • 2





                          @Allen - JavaScript doesn't support the s modifier. Instead, do [^]* for the same effect.

                          – Derek 朕會功夫
                          Jul 12 '15 at 22:26






                        • 1





                          In Ruby, use the m modifier

                          – Ryan Buckley
                          Jul 15 '15 at 22:57














                        • 3





                          @Grace: use n to match a newline

                          – Jeremy Ruten
                          Apr 11 '11 at 21:05






                        • 5





                          rn works perfectly

                          – Grace
                          Apr 12 '11 at 8:08






                        • 2





                          The s flag is (now?) invalid, at least in Chrome/V8. Instead use /([sS]*)<FooBar>/ character class (match space and non-space] instead of the period matcher. See other answers for more info.

                          – Allen
                          May 9 '13 at 15:37








                        • 2





                          @Allen - JavaScript doesn't support the s modifier. Instead, do [^]* for the same effect.

                          – Derek 朕會功夫
                          Jul 12 '15 at 22:26






                        • 1





                          In Ruby, use the m modifier

                          – Ryan Buckley
                          Jul 15 '15 at 22:57








                        3




                        3





                        @Grace: use n to match a newline

                        – Jeremy Ruten
                        Apr 11 '11 at 21:05





                        @Grace: use n to match a newline

                        – Jeremy Ruten
                        Apr 11 '11 at 21:05




                        5




                        5





                        rn works perfectly

                        – Grace
                        Apr 12 '11 at 8:08





                        rn works perfectly

                        – Grace
                        Apr 12 '11 at 8:08




                        2




                        2





                        The s flag is (now?) invalid, at least in Chrome/V8. Instead use /([sS]*)<FooBar>/ character class (match space and non-space] instead of the period matcher. See other answers for more info.

                        – Allen
                        May 9 '13 at 15:37







                        The s flag is (now?) invalid, at least in Chrome/V8. Instead use /([sS]*)<FooBar>/ character class (match space and non-space] instead of the period matcher. See other answers for more info.

                        – Allen
                        May 9 '13 at 15:37






                        2




                        2





                        @Allen - JavaScript doesn't support the s modifier. Instead, do [^]* for the same effect.

                        – Derek 朕會功夫
                        Jul 12 '15 at 22:26





                        @Allen - JavaScript doesn't support the s modifier. Instead, do [^]* for the same effect.

                        – Derek 朕會功夫
                        Jul 12 '15 at 22:26




                        1




                        1





                        In Ruby, use the m modifier

                        – Ryan Buckley
                        Jul 15 '15 at 22:57





                        In Ruby, use the m modifier

                        – Ryan Buckley
                        Jul 15 '15 at 22:57













                        264














                        Try this:



                        ((.|n)*)<FooBar>


                        It basically says "any character or a newline" repeated zero or more times.






                        share|improve this answer



















                        • 3





                          This is dependent on the language and/or tool you are using. Please let us know what you are using, eg Perl, PHP, CF, C#, sed, awk, etc.

                          – Ben Doom
                          Oct 1 '08 at 18:57






                        • 31





                          Depending on your line endings you might need ((.|n|r)*)<FooBar>

                          – Potherca
                          Mar 9 '12 at 17:27






                        • 3





                          He said he is using Eclipse. This is correct solution in my opinion. I have same problem and this solved it.

                          – Danubian Sailor
                          Apr 18 '12 at 8:14






                        • 4





                          Right - the question is about eclipse and so are the tags. But the accepted solution is a PHP solution. Yours should be the accepted solution...

                          – acme
                          Jun 13 '12 at 12:04






                        • 6





                          This is the worst regex for matching multiple line input. Please never use it unless you are using ElasticSearch. Use [sS]* or (?s).*.

                          – Wiktor Stribiżew
                          Jul 18 '16 at 11:05


















                        264














                        Try this:



                        ((.|n)*)<FooBar>


                        It basically says "any character or a newline" repeated zero or more times.






                        share|improve this answer



















                        • 3





                          This is dependent on the language and/or tool you are using. Please let us know what you are using, eg Perl, PHP, CF, C#, sed, awk, etc.

                          – Ben Doom
                          Oct 1 '08 at 18:57






                        • 31





                          Depending on your line endings you might need ((.|n|r)*)<FooBar>

                          – Potherca
                          Mar 9 '12 at 17:27






                        • 3





                          He said he is using Eclipse. This is correct solution in my opinion. I have same problem and this solved it.

                          – Danubian Sailor
                          Apr 18 '12 at 8:14






                        • 4





                          Right - the question is about eclipse and so are the tags. But the accepted solution is a PHP solution. Yours should be the accepted solution...

                          – acme
                          Jun 13 '12 at 12:04






                        • 6





                          This is the worst regex for matching multiple line input. Please never use it unless you are using ElasticSearch. Use [sS]* or (?s).*.

                          – Wiktor Stribiżew
                          Jul 18 '16 at 11:05
















                        264












                        264








                        264







                        Try this:



                        ((.|n)*)<FooBar>


                        It basically says "any character or a newline" repeated zero or more times.






                        share|improve this answer













                        Try this:



                        ((.|n)*)<FooBar>


                        It basically says "any character or a newline" repeated zero or more times.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 1 '08 at 18:52









                        leviklevik

                        72.9k236689




                        72.9k236689








                        • 3





                          This is dependent on the language and/or tool you are using. Please let us know what you are using, eg Perl, PHP, CF, C#, sed, awk, etc.

                          – Ben Doom
                          Oct 1 '08 at 18:57






                        • 31





                          Depending on your line endings you might need ((.|n|r)*)<FooBar>

                          – Potherca
                          Mar 9 '12 at 17:27






                        • 3





                          He said he is using Eclipse. This is correct solution in my opinion. I have same problem and this solved it.

                          – Danubian Sailor
                          Apr 18 '12 at 8:14






                        • 4





                          Right - the question is about eclipse and so are the tags. But the accepted solution is a PHP solution. Yours should be the accepted solution...

                          – acme
                          Jun 13 '12 at 12:04






                        • 6





                          This is the worst regex for matching multiple line input. Please never use it unless you are using ElasticSearch. Use [sS]* or (?s).*.

                          – Wiktor Stribiżew
                          Jul 18 '16 at 11:05
















                        • 3





                          This is dependent on the language and/or tool you are using. Please let us know what you are using, eg Perl, PHP, CF, C#, sed, awk, etc.

                          – Ben Doom
                          Oct 1 '08 at 18:57






                        • 31





                          Depending on your line endings you might need ((.|n|r)*)<FooBar>

                          – Potherca
                          Mar 9 '12 at 17:27






                        • 3





                          He said he is using Eclipse. This is correct solution in my opinion. I have same problem and this solved it.

                          – Danubian Sailor
                          Apr 18 '12 at 8:14






                        • 4





                          Right - the question is about eclipse and so are the tags. But the accepted solution is a PHP solution. Yours should be the accepted solution...

                          – acme
                          Jun 13 '12 at 12:04






                        • 6





                          This is the worst regex for matching multiple line input. Please never use it unless you are using ElasticSearch. Use [sS]* or (?s).*.

                          – Wiktor Stribiżew
                          Jul 18 '16 at 11:05










                        3




                        3





                        This is dependent on the language and/or tool you are using. Please let us know what you are using, eg Perl, PHP, CF, C#, sed, awk, etc.

                        – Ben Doom
                        Oct 1 '08 at 18:57





                        This is dependent on the language and/or tool you are using. Please let us know what you are using, eg Perl, PHP, CF, C#, sed, awk, etc.

                        – Ben Doom
                        Oct 1 '08 at 18:57




                        31




                        31





                        Depending on your line endings you might need ((.|n|r)*)<FooBar>

                        – Potherca
                        Mar 9 '12 at 17:27





                        Depending on your line endings you might need ((.|n|r)*)<FooBar>

                        – Potherca
                        Mar 9 '12 at 17:27




                        3




                        3





                        He said he is using Eclipse. This is correct solution in my opinion. I have same problem and this solved it.

                        – Danubian Sailor
                        Apr 18 '12 at 8:14





                        He said he is using Eclipse. This is correct solution in my opinion. I have same problem and this solved it.

                        – Danubian Sailor
                        Apr 18 '12 at 8:14




                        4




                        4





                        Right - the question is about eclipse and so are the tags. But the accepted solution is a PHP solution. Yours should be the accepted solution...

                        – acme
                        Jun 13 '12 at 12:04





                        Right - the question is about eclipse and so are the tags. But the accepted solution is a PHP solution. Yours should be the accepted solution...

                        – acme
                        Jun 13 '12 at 12:04




                        6




                        6





                        This is the worst regex for matching multiple line input. Please never use it unless you are using ElasticSearch. Use [sS]* or (?s).*.

                        – Wiktor Stribiżew
                        Jul 18 '16 at 11:05







                        This is the worst regex for matching multiple line input. Please never use it unless you are using ElasticSearch. Use [sS]* or (?s).*.

                        – Wiktor Stribiżew
                        Jul 18 '16 at 11:05













                        61














                        If you're using Eclipse search, you can enable the "DOTALL" option to make '.' match any character including line delimiters: just add "(?s)" at the beginning of your search string. Example:



                        (?s).*<FooBar>





                        share|improve this answer



















                        • 8





                          This is not eclipse-specific, should work anywhere.

                          – Steven Soroka
                          Oct 8 '13 at 16:50











                        • Not anywhere, only in regex flavors supporting inline modifiers, and certainly not in Ruby where (?s) => (?m)

                          – Wiktor Stribiżew
                          Jul 18 '16 at 11:06











                        • Anything for bash?

                          – Pasupathi Rajamanickam
                          Dec 19 '18 at 2:12
















                        61














                        If you're using Eclipse search, you can enable the "DOTALL" option to make '.' match any character including line delimiters: just add "(?s)" at the beginning of your search string. Example:



                        (?s).*<FooBar>





                        share|improve this answer



















                        • 8





                          This is not eclipse-specific, should work anywhere.

                          – Steven Soroka
                          Oct 8 '13 at 16:50











                        • Not anywhere, only in regex flavors supporting inline modifiers, and certainly not in Ruby where (?s) => (?m)

                          – Wiktor Stribiżew
                          Jul 18 '16 at 11:06











                        • Anything for bash?

                          – Pasupathi Rajamanickam
                          Dec 19 '18 at 2:12














                        61












                        61








                        61







                        If you're using Eclipse search, you can enable the "DOTALL" option to make '.' match any character including line delimiters: just add "(?s)" at the beginning of your search string. Example:



                        (?s).*<FooBar>





                        share|improve this answer













                        If you're using Eclipse search, you can enable the "DOTALL" option to make '.' match any character including line delimiters: just add "(?s)" at the beginning of your search string. Example:



                        (?s).*<FooBar>






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 25 '11 at 13:16









                        Paulo MersonPaulo Merson

                        5,40143236




                        5,40143236








                        • 8





                          This is not eclipse-specific, should work anywhere.

                          – Steven Soroka
                          Oct 8 '13 at 16:50











                        • Not anywhere, only in regex flavors supporting inline modifiers, and certainly not in Ruby where (?s) => (?m)

                          – Wiktor Stribiżew
                          Jul 18 '16 at 11:06











                        • Anything for bash?

                          – Pasupathi Rajamanickam
                          Dec 19 '18 at 2:12














                        • 8





                          This is not eclipse-specific, should work anywhere.

                          – Steven Soroka
                          Oct 8 '13 at 16:50











                        • Not anywhere, only in regex flavors supporting inline modifiers, and certainly not in Ruby where (?s) => (?m)

                          – Wiktor Stribiżew
                          Jul 18 '16 at 11:06











                        • Anything for bash?

                          – Pasupathi Rajamanickam
                          Dec 19 '18 at 2:12








                        8




                        8





                        This is not eclipse-specific, should work anywhere.

                        – Steven Soroka
                        Oct 8 '13 at 16:50





                        This is not eclipse-specific, should work anywhere.

                        – Steven Soroka
                        Oct 8 '13 at 16:50













                        Not anywhere, only in regex flavors supporting inline modifiers, and certainly not in Ruby where (?s) => (?m)

                        – Wiktor Stribiżew
                        Jul 18 '16 at 11:06





                        Not anywhere, only in regex flavors supporting inline modifiers, and certainly not in Ruby where (?s) => (?m)

                        – Wiktor Stribiżew
                        Jul 18 '16 at 11:06













                        Anything for bash?

                        – Pasupathi Rajamanickam
                        Dec 19 '18 at 2:12





                        Anything for bash?

                        – Pasupathi Rajamanickam
                        Dec 19 '18 at 2:12











                        46














                        The question is, can . pattern match any character? The answer varies from engine to engine. The main difference is whether the pattern is used by a POSIX or non-POSIX regex library.



                        Special note about lua-patterns: they are not considered regular expressions, but . matches any char there, same as POSIX based engines.



                        Another note on matlab and octave: the . matches any char by default (demo): str = "abcden fghij<Foobar>"; expression = '(.*)<Foobar>*'; [tokens,matches] = regexp(str,expression,'tokens','match'); (tokens contain a abcden fghij item).



                        Also, in all of boost's regex grammars the dot matches line breaks by default. Boost's ECMAScript grammar allows you to turn this off with regex_constants::no_mod_m (source).



                        As for oracle (it is POSIX based), use n option (demo): select regexp_substr('abcde' || chr(10) ||' fghij<Foobar>', '(.*)<Foobar>', 1, 1, 'n', 1) as results from dual



                        POSIX-based engines:



                        A mere . already matches line breaks, no need to use any modifiers, see bash (demo).



                        The tcl (demo), postgresql (demo), r (TRE, base R default engine with no perl=TRUE, for base R with perl=TRUE or for stringr/stringi patterns, use the (?s) inline modifier) (demo) also treat . the same way.



                        However, most POSIX based tools process input line by line. Hence, . does not match the line breaks just because they are not in scope. Here are some examples how to override this:





                        • sed - There are multiple workarounds, the most precise but not very safe is sed 'H;1h;$!d;x; s/(.*)><Foobar>/1/' (H;1h;$!d;x; slurps the file into memory). If whole lines must be included, sed '/start_pattern/,/end_pattern/d' file (removing from start will end with matched lines included) or sed '/start_pattern/,/end_pattern/{{//!d;};}' file (with matching lines excluded) can be considered.


                        • perl - perl -0pe 's/(.*)<FooBar>/$1/gs' <<< "$str" (-0 slurps the whole file into memory, -p prints the file after applying the script given by -e). Note that using -000pe will slurp the file and activate 'paragraph mode' where Perl uses consecutive newlines (nn) as the record separator.


                        • gnu-grep - grep -Poz '(?si)abcK.*?(?=<Foobar>)' file. Here, z enables file slurping, (?s) enables the DOTALL mode for the . pattern, (?i) enables case insensitive mode, K omits the text matched so far, *? is a lazy quantifier, (?=<Foobar>) matches the location before <Foobar>.


                        • pcregrep - pcregrep -Mi "(?si)abcK.*?(?=<Foobar>)" file (M enables file slurping here). Note pcregrep is a good solution for Mac OS grep users.


                        See demos.



                        Non-POSIX-based engines:





                        • php - Use s modifier PCRE_DOTALL modifier: preg_match('~(.*)<Foobar>~s', $s, $m) (demo)


                        • c# - Use RegexOptions.Singleline flag (demo):
                          - var result = Regex.Match(s, @"(.*)<Foobar>", RegexOptions.Singleline).Groups[1].Value;
                          - var result = Regex.Match(s, @"(?s)(.*)<Foobar>").Groups[1].Value;


                        • powershell - Use (?s) inline option: $s = "abcde`nfghij<FooBar>"; $s -match "(?s)(.*)<Foobar>"; $matches[1]


                        • perl - Use s modifier (or (?s) inline version at the start) (demo): /(.*)<FooBar>/s


                        • python - Use re.DOTALL (or re.S) flags or (?s) inline modifier (demo): m = re.search(r"(.*)<FooBar>", s, flags=re.S) (and then if m:, print(m.group(1)))


                        • java - Use Pattern.DOTALL modifier (or inline (?s) flag) (demo): Pattern.compile("(.*)<FooBar>", Pattern.DOTALL)


                        • groovy - Use (?s) in-pattern modifier (demo): regex = /(?s)(.*)<FooBar>/


                        • scala - Use (?s) modifier (demo): "(?s)(.*)<Foobar>".r.findAllIn("abcden fghij<Foobar>").matchData foreach { m => println(m.group(1)) }


                        • javascript - Use [^] or workarounds [dD] / [wW] / [sS] (demo): s.match(/([sS]*)<FooBar>/)[1]


                        • c++ (std::regex) Use [sS] or the JS workarounds (demo): regex rex(R"(([sS]*)<FooBar>)");


                        • vba - Use the same approach as in JavaScript, ([sS]*)<Foobar>.


                        • ruby - Use /m MULTILINE modifier (demo): s[/(.*)<Foobar>/m, 1]


                        • go - Use the inline modifier (?s) at the start (demo): re: = regexp.MustCompile(`(?s)(.*)<FooBar>`)


                        • swift - Use dotMatchesLineSeparators or (easier) pass the (?s) inline modifier to the pattern: let rx = "(?s)(.*)<Foobar>"


                        • objective-c - Same as Swift, (?s) works the easiest, but here is how the option can be used: NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern
                          options:NSRegularExpressionDotMatchesLineSeparators error:&regexError];



                        • re2, google-apps-script - Use (?s) modifier (demo): "(?s)(.*)<Foobar>" (in Google Spreadsheets, =REGEXEXTRACT(A2,"(?s)(.*)<Foobar>"))


                        NOTES ON (?s):



                        In most non-POSIX engines, (?s) inline modifier (or embedded flag option) can be used to enforce . to match line breaks.



                        If placed at the start of the pattern, (?s) changes the bahavior of all . in the pattern. If the (?s) is placed somewhere after the beginning, only those . will be affected that are located to the right of it unless this is a pattern passed to Python re. In Python re, regardless of the (?s) location, the whole pattern . are affected. The (?s) effect is stopped using (?-s). A modified group can be used to only affect a specified range of a regex pattern (e.g. Delim1(?s:.*?)nDelim2.* will make the first .*? match across newlines and the second .* will only match the rest of the line).



                        POSIX note:



                        In non-regex engines, to match any char, [sS] / [dD] / [wW] constructs can be used.



                        In POSIX, [sS] is not matching any char (as in JavaScript or any non-POSIX engine) because regex escape sequences are not supported inside bracket expressions. [sS] is parsed as bracket expressions that match a single char, or s or S.






                        share|improve this answer





















                        • 4





                          You should link to this excellent overview from your profile page or something (+1).

                          – Jan
                          Oct 15 '17 at 20:15






                        • 1





                          You may want to add this to the boost item: In the regex_constants namespace, flag_type_'s : perl = ECMAScript = JavaScript = JScript = ::boost::regbase::normal = 0 which defaults to Perl. Programmers will set a base flag definition #define MOD regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m for thier regex flags to reflect that. And the arbitor is always the inline modifiers. Where (?-sm)(?s).* resets.

                          – sln
                          Apr 26 '18 at 21:30








                        • 1





                          Can you also add for bash please?

                          – Pasupathi Rajamanickam
                          Dec 19 '18 at 2:12






                        • 2





                          @PasupathiRajamanickam Bash uses a POSIX regex engine, the . matches any char there (including line breaks). See this online Bash demo.

                          – Wiktor Stribiżew
                          Dec 19 '18 at 7:33


















                        46














                        The question is, can . pattern match any character? The answer varies from engine to engine. The main difference is whether the pattern is used by a POSIX or non-POSIX regex library.



                        Special note about lua-patterns: they are not considered regular expressions, but . matches any char there, same as POSIX based engines.



                        Another note on matlab and octave: the . matches any char by default (demo): str = "abcden fghij<Foobar>"; expression = '(.*)<Foobar>*'; [tokens,matches] = regexp(str,expression,'tokens','match'); (tokens contain a abcden fghij item).



                        Also, in all of boost's regex grammars the dot matches line breaks by default. Boost's ECMAScript grammar allows you to turn this off with regex_constants::no_mod_m (source).



                        As for oracle (it is POSIX based), use n option (demo): select regexp_substr('abcde' || chr(10) ||' fghij<Foobar>', '(.*)<Foobar>', 1, 1, 'n', 1) as results from dual



                        POSIX-based engines:



                        A mere . already matches line breaks, no need to use any modifiers, see bash (demo).



                        The tcl (demo), postgresql (demo), r (TRE, base R default engine with no perl=TRUE, for base R with perl=TRUE or for stringr/stringi patterns, use the (?s) inline modifier) (demo) also treat . the same way.



                        However, most POSIX based tools process input line by line. Hence, . does not match the line breaks just because they are not in scope. Here are some examples how to override this:





                        • sed - There are multiple workarounds, the most precise but not very safe is sed 'H;1h;$!d;x; s/(.*)><Foobar>/1/' (H;1h;$!d;x; slurps the file into memory). If whole lines must be included, sed '/start_pattern/,/end_pattern/d' file (removing from start will end with matched lines included) or sed '/start_pattern/,/end_pattern/{{//!d;};}' file (with matching lines excluded) can be considered.


                        • perl - perl -0pe 's/(.*)<FooBar>/$1/gs' <<< "$str" (-0 slurps the whole file into memory, -p prints the file after applying the script given by -e). Note that using -000pe will slurp the file and activate 'paragraph mode' where Perl uses consecutive newlines (nn) as the record separator.


                        • gnu-grep - grep -Poz '(?si)abcK.*?(?=<Foobar>)' file. Here, z enables file slurping, (?s) enables the DOTALL mode for the . pattern, (?i) enables case insensitive mode, K omits the text matched so far, *? is a lazy quantifier, (?=<Foobar>) matches the location before <Foobar>.


                        • pcregrep - pcregrep -Mi "(?si)abcK.*?(?=<Foobar>)" file (M enables file slurping here). Note pcregrep is a good solution for Mac OS grep users.


                        See demos.



                        Non-POSIX-based engines:





                        • php - Use s modifier PCRE_DOTALL modifier: preg_match('~(.*)<Foobar>~s', $s, $m) (demo)


                        • c# - Use RegexOptions.Singleline flag (demo):
                          - var result = Regex.Match(s, @"(.*)<Foobar>", RegexOptions.Singleline).Groups[1].Value;
                          - var result = Regex.Match(s, @"(?s)(.*)<Foobar>").Groups[1].Value;


                        • powershell - Use (?s) inline option: $s = "abcde`nfghij<FooBar>"; $s -match "(?s)(.*)<Foobar>"; $matches[1]


                        • perl - Use s modifier (or (?s) inline version at the start) (demo): /(.*)<FooBar>/s


                        • python - Use re.DOTALL (or re.S) flags or (?s) inline modifier (demo): m = re.search(r"(.*)<FooBar>", s, flags=re.S) (and then if m:, print(m.group(1)))


                        • java - Use Pattern.DOTALL modifier (or inline (?s) flag) (demo): Pattern.compile("(.*)<FooBar>", Pattern.DOTALL)


                        • groovy - Use (?s) in-pattern modifier (demo): regex = /(?s)(.*)<FooBar>/


                        • scala - Use (?s) modifier (demo): "(?s)(.*)<Foobar>".r.findAllIn("abcden fghij<Foobar>").matchData foreach { m => println(m.group(1)) }


                        • javascript - Use [^] or workarounds [dD] / [wW] / [sS] (demo): s.match(/([sS]*)<FooBar>/)[1]


                        • c++ (std::regex) Use [sS] or the JS workarounds (demo): regex rex(R"(([sS]*)<FooBar>)");


                        • vba - Use the same approach as in JavaScript, ([sS]*)<Foobar>.


                        • ruby - Use /m MULTILINE modifier (demo): s[/(.*)<Foobar>/m, 1]


                        • go - Use the inline modifier (?s) at the start (demo): re: = regexp.MustCompile(`(?s)(.*)<FooBar>`)


                        • swift - Use dotMatchesLineSeparators or (easier) pass the (?s) inline modifier to the pattern: let rx = "(?s)(.*)<Foobar>"


                        • objective-c - Same as Swift, (?s) works the easiest, but here is how the option can be used: NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern
                          options:NSRegularExpressionDotMatchesLineSeparators error:&regexError];



                        • re2, google-apps-script - Use (?s) modifier (demo): "(?s)(.*)<Foobar>" (in Google Spreadsheets, =REGEXEXTRACT(A2,"(?s)(.*)<Foobar>"))


                        NOTES ON (?s):



                        In most non-POSIX engines, (?s) inline modifier (or embedded flag option) can be used to enforce . to match line breaks.



                        If placed at the start of the pattern, (?s) changes the bahavior of all . in the pattern. If the (?s) is placed somewhere after the beginning, only those . will be affected that are located to the right of it unless this is a pattern passed to Python re. In Python re, regardless of the (?s) location, the whole pattern . are affected. The (?s) effect is stopped using (?-s). A modified group can be used to only affect a specified range of a regex pattern (e.g. Delim1(?s:.*?)nDelim2.* will make the first .*? match across newlines and the second .* will only match the rest of the line).



                        POSIX note:



                        In non-regex engines, to match any char, [sS] / [dD] / [wW] constructs can be used.



                        In POSIX, [sS] is not matching any char (as in JavaScript or any non-POSIX engine) because regex escape sequences are not supported inside bracket expressions. [sS] is parsed as bracket expressions that match a single char, or s or S.






                        share|improve this answer





















                        • 4





                          You should link to this excellent overview from your profile page or something (+1).

                          – Jan
                          Oct 15 '17 at 20:15






                        • 1





                          You may want to add this to the boost item: In the regex_constants namespace, flag_type_'s : perl = ECMAScript = JavaScript = JScript = ::boost::regbase::normal = 0 which defaults to Perl. Programmers will set a base flag definition #define MOD regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m for thier regex flags to reflect that. And the arbitor is always the inline modifiers. Where (?-sm)(?s).* resets.

                          – sln
                          Apr 26 '18 at 21:30








                        • 1





                          Can you also add for bash please?

                          – Pasupathi Rajamanickam
                          Dec 19 '18 at 2:12






                        • 2





                          @PasupathiRajamanickam Bash uses a POSIX regex engine, the . matches any char there (including line breaks). See this online Bash demo.

                          – Wiktor Stribiżew
                          Dec 19 '18 at 7:33
















                        46












                        46








                        46







                        The question is, can . pattern match any character? The answer varies from engine to engine. The main difference is whether the pattern is used by a POSIX or non-POSIX regex library.



                        Special note about lua-patterns: they are not considered regular expressions, but . matches any char there, same as POSIX based engines.



                        Another note on matlab and octave: the . matches any char by default (demo): str = "abcden fghij<Foobar>"; expression = '(.*)<Foobar>*'; [tokens,matches] = regexp(str,expression,'tokens','match'); (tokens contain a abcden fghij item).



                        Also, in all of boost's regex grammars the dot matches line breaks by default. Boost's ECMAScript grammar allows you to turn this off with regex_constants::no_mod_m (source).



                        As for oracle (it is POSIX based), use n option (demo): select regexp_substr('abcde' || chr(10) ||' fghij<Foobar>', '(.*)<Foobar>', 1, 1, 'n', 1) as results from dual



                        POSIX-based engines:



                        A mere . already matches line breaks, no need to use any modifiers, see bash (demo).



                        The tcl (demo), postgresql (demo), r (TRE, base R default engine with no perl=TRUE, for base R with perl=TRUE or for stringr/stringi patterns, use the (?s) inline modifier) (demo) also treat . the same way.



                        However, most POSIX based tools process input line by line. Hence, . does not match the line breaks just because they are not in scope. Here are some examples how to override this:





                        • sed - There are multiple workarounds, the most precise but not very safe is sed 'H;1h;$!d;x; s/(.*)><Foobar>/1/' (H;1h;$!d;x; slurps the file into memory). If whole lines must be included, sed '/start_pattern/,/end_pattern/d' file (removing from start will end with matched lines included) or sed '/start_pattern/,/end_pattern/{{//!d;};}' file (with matching lines excluded) can be considered.


                        • perl - perl -0pe 's/(.*)<FooBar>/$1/gs' <<< "$str" (-0 slurps the whole file into memory, -p prints the file after applying the script given by -e). Note that using -000pe will slurp the file and activate 'paragraph mode' where Perl uses consecutive newlines (nn) as the record separator.


                        • gnu-grep - grep -Poz '(?si)abcK.*?(?=<Foobar>)' file. Here, z enables file slurping, (?s) enables the DOTALL mode for the . pattern, (?i) enables case insensitive mode, K omits the text matched so far, *? is a lazy quantifier, (?=<Foobar>) matches the location before <Foobar>.


                        • pcregrep - pcregrep -Mi "(?si)abcK.*?(?=<Foobar>)" file (M enables file slurping here). Note pcregrep is a good solution for Mac OS grep users.


                        See demos.



                        Non-POSIX-based engines:





                        • php - Use s modifier PCRE_DOTALL modifier: preg_match('~(.*)<Foobar>~s', $s, $m) (demo)


                        • c# - Use RegexOptions.Singleline flag (demo):
                          - var result = Regex.Match(s, @"(.*)<Foobar>", RegexOptions.Singleline).Groups[1].Value;
                          - var result = Regex.Match(s, @"(?s)(.*)<Foobar>").Groups[1].Value;


                        • powershell - Use (?s) inline option: $s = "abcde`nfghij<FooBar>"; $s -match "(?s)(.*)<Foobar>"; $matches[1]


                        • perl - Use s modifier (or (?s) inline version at the start) (demo): /(.*)<FooBar>/s


                        • python - Use re.DOTALL (or re.S) flags or (?s) inline modifier (demo): m = re.search(r"(.*)<FooBar>", s, flags=re.S) (and then if m:, print(m.group(1)))


                        • java - Use Pattern.DOTALL modifier (or inline (?s) flag) (demo): Pattern.compile("(.*)<FooBar>", Pattern.DOTALL)


                        • groovy - Use (?s) in-pattern modifier (demo): regex = /(?s)(.*)<FooBar>/


                        • scala - Use (?s) modifier (demo): "(?s)(.*)<Foobar>".r.findAllIn("abcden fghij<Foobar>").matchData foreach { m => println(m.group(1)) }


                        • javascript - Use [^] or workarounds [dD] / [wW] / [sS] (demo): s.match(/([sS]*)<FooBar>/)[1]


                        • c++ (std::regex) Use [sS] or the JS workarounds (demo): regex rex(R"(([sS]*)<FooBar>)");


                        • vba - Use the same approach as in JavaScript, ([sS]*)<Foobar>.


                        • ruby - Use /m MULTILINE modifier (demo): s[/(.*)<Foobar>/m, 1]


                        • go - Use the inline modifier (?s) at the start (demo): re: = regexp.MustCompile(`(?s)(.*)<FooBar>`)


                        • swift - Use dotMatchesLineSeparators or (easier) pass the (?s) inline modifier to the pattern: let rx = "(?s)(.*)<Foobar>"


                        • objective-c - Same as Swift, (?s) works the easiest, but here is how the option can be used: NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern
                          options:NSRegularExpressionDotMatchesLineSeparators error:&regexError];



                        • re2, google-apps-script - Use (?s) modifier (demo): "(?s)(.*)<Foobar>" (in Google Spreadsheets, =REGEXEXTRACT(A2,"(?s)(.*)<Foobar>"))


                        NOTES ON (?s):



                        In most non-POSIX engines, (?s) inline modifier (or embedded flag option) can be used to enforce . to match line breaks.



                        If placed at the start of the pattern, (?s) changes the bahavior of all . in the pattern. If the (?s) is placed somewhere after the beginning, only those . will be affected that are located to the right of it unless this is a pattern passed to Python re. In Python re, regardless of the (?s) location, the whole pattern . are affected. The (?s) effect is stopped using (?-s). A modified group can be used to only affect a specified range of a regex pattern (e.g. Delim1(?s:.*?)nDelim2.* will make the first .*? match across newlines and the second .* will only match the rest of the line).



                        POSIX note:



                        In non-regex engines, to match any char, [sS] / [dD] / [wW] constructs can be used.



                        In POSIX, [sS] is not matching any char (as in JavaScript or any non-POSIX engine) because regex escape sequences are not supported inside bracket expressions. [sS] is parsed as bracket expressions that match a single char, or s or S.






                        share|improve this answer















                        The question is, can . pattern match any character? The answer varies from engine to engine. The main difference is whether the pattern is used by a POSIX or non-POSIX regex library.



                        Special note about lua-patterns: they are not considered regular expressions, but . matches any char there, same as POSIX based engines.



                        Another note on matlab and octave: the . matches any char by default (demo): str = "abcden fghij<Foobar>"; expression = '(.*)<Foobar>*'; [tokens,matches] = regexp(str,expression,'tokens','match'); (tokens contain a abcden fghij item).



                        Also, in all of boost's regex grammars the dot matches line breaks by default. Boost's ECMAScript grammar allows you to turn this off with regex_constants::no_mod_m (source).



                        As for oracle (it is POSIX based), use n option (demo): select regexp_substr('abcde' || chr(10) ||' fghij<Foobar>', '(.*)<Foobar>', 1, 1, 'n', 1) as results from dual



                        POSIX-based engines:



                        A mere . already matches line breaks, no need to use any modifiers, see bash (demo).



                        The tcl (demo), postgresql (demo), r (TRE, base R default engine with no perl=TRUE, for base R with perl=TRUE or for stringr/stringi patterns, use the (?s) inline modifier) (demo) also treat . the same way.



                        However, most POSIX based tools process input line by line. Hence, . does not match the line breaks just because they are not in scope. Here are some examples how to override this:





                        • sed - There are multiple workarounds, the most precise but not very safe is sed 'H;1h;$!d;x; s/(.*)><Foobar>/1/' (H;1h;$!d;x; slurps the file into memory). If whole lines must be included, sed '/start_pattern/,/end_pattern/d' file (removing from start will end with matched lines included) or sed '/start_pattern/,/end_pattern/{{//!d;};}' file (with matching lines excluded) can be considered.


                        • perl - perl -0pe 's/(.*)<FooBar>/$1/gs' <<< "$str" (-0 slurps the whole file into memory, -p prints the file after applying the script given by -e). Note that using -000pe will slurp the file and activate 'paragraph mode' where Perl uses consecutive newlines (nn) as the record separator.


                        • gnu-grep - grep -Poz '(?si)abcK.*?(?=<Foobar>)' file. Here, z enables file slurping, (?s) enables the DOTALL mode for the . pattern, (?i) enables case insensitive mode, K omits the text matched so far, *? is a lazy quantifier, (?=<Foobar>) matches the location before <Foobar>.


                        • pcregrep - pcregrep -Mi "(?si)abcK.*?(?=<Foobar>)" file (M enables file slurping here). Note pcregrep is a good solution for Mac OS grep users.


                        See demos.



                        Non-POSIX-based engines:





                        • php - Use s modifier PCRE_DOTALL modifier: preg_match('~(.*)<Foobar>~s', $s, $m) (demo)


                        • c# - Use RegexOptions.Singleline flag (demo):
                          - var result = Regex.Match(s, @"(.*)<Foobar>", RegexOptions.Singleline).Groups[1].Value;
                          - var result = Regex.Match(s, @"(?s)(.*)<Foobar>").Groups[1].Value;


                        • powershell - Use (?s) inline option: $s = "abcde`nfghij<FooBar>"; $s -match "(?s)(.*)<Foobar>"; $matches[1]


                        • perl - Use s modifier (or (?s) inline version at the start) (demo): /(.*)<FooBar>/s


                        • python - Use re.DOTALL (or re.S) flags or (?s) inline modifier (demo): m = re.search(r"(.*)<FooBar>", s, flags=re.S) (and then if m:, print(m.group(1)))


                        • java - Use Pattern.DOTALL modifier (or inline (?s) flag) (demo): Pattern.compile("(.*)<FooBar>", Pattern.DOTALL)


                        • groovy - Use (?s) in-pattern modifier (demo): regex = /(?s)(.*)<FooBar>/


                        • scala - Use (?s) modifier (demo): "(?s)(.*)<Foobar>".r.findAllIn("abcden fghij<Foobar>").matchData foreach { m => println(m.group(1)) }


                        • javascript - Use [^] or workarounds [dD] / [wW] / [sS] (demo): s.match(/([sS]*)<FooBar>/)[1]


                        • c++ (std::regex) Use [sS] or the JS workarounds (demo): regex rex(R"(([sS]*)<FooBar>)");


                        • vba - Use the same approach as in JavaScript, ([sS]*)<Foobar>.


                        • ruby - Use /m MULTILINE modifier (demo): s[/(.*)<Foobar>/m, 1]


                        • go - Use the inline modifier (?s) at the start (demo): re: = regexp.MustCompile(`(?s)(.*)<FooBar>`)


                        • swift - Use dotMatchesLineSeparators or (easier) pass the (?s) inline modifier to the pattern: let rx = "(?s)(.*)<Foobar>"


                        • objective-c - Same as Swift, (?s) works the easiest, but here is how the option can be used: NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern
                          options:NSRegularExpressionDotMatchesLineSeparators error:&regexError];



                        • re2, google-apps-script - Use (?s) modifier (demo): "(?s)(.*)<Foobar>" (in Google Spreadsheets, =REGEXEXTRACT(A2,"(?s)(.*)<Foobar>"))


                        NOTES ON (?s):



                        In most non-POSIX engines, (?s) inline modifier (or embedded flag option) can be used to enforce . to match line breaks.



                        If placed at the start of the pattern, (?s) changes the bahavior of all . in the pattern. If the (?s) is placed somewhere after the beginning, only those . will be affected that are located to the right of it unless this is a pattern passed to Python re. In Python re, regardless of the (?s) location, the whole pattern . are affected. The (?s) effect is stopped using (?-s). A modified group can be used to only affect a specified range of a regex pattern (e.g. Delim1(?s:.*?)nDelim2.* will make the first .*? match across newlines and the second .* will only match the rest of the line).



                        POSIX note:



                        In non-regex engines, to match any char, [sS] / [dD] / [wW] constructs can be used.



                        In POSIX, [sS] is not matching any char (as in JavaScript or any non-POSIX engine) because regex escape sequences are not supported inside bracket expressions. [sS] is parsed as bracket expressions that match a single char, or s or S.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Dec 20 '18 at 9:49

























                        answered Aug 31 '17 at 12:47









                        Wiktor StribiżewWiktor Stribiżew

                        320k16140222




                        320k16140222








                        • 4





                          You should link to this excellent overview from your profile page or something (+1).

                          – Jan
                          Oct 15 '17 at 20:15






                        • 1





                          You may want to add this to the boost item: In the regex_constants namespace, flag_type_'s : perl = ECMAScript = JavaScript = JScript = ::boost::regbase::normal = 0 which defaults to Perl. Programmers will set a base flag definition #define MOD regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m for thier regex flags to reflect that. And the arbitor is always the inline modifiers. Where (?-sm)(?s).* resets.

                          – sln
                          Apr 26 '18 at 21:30








                        • 1





                          Can you also add for bash please?

                          – Pasupathi Rajamanickam
                          Dec 19 '18 at 2:12






                        • 2





                          @PasupathiRajamanickam Bash uses a POSIX regex engine, the . matches any char there (including line breaks). See this online Bash demo.

                          – Wiktor Stribiżew
                          Dec 19 '18 at 7:33
















                        • 4





                          You should link to this excellent overview from your profile page or something (+1).

                          – Jan
                          Oct 15 '17 at 20:15






                        • 1





                          You may want to add this to the boost item: In the regex_constants namespace, flag_type_'s : perl = ECMAScript = JavaScript = JScript = ::boost::regbase::normal = 0 which defaults to Perl. Programmers will set a base flag definition #define MOD regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m for thier regex flags to reflect that. And the arbitor is always the inline modifiers. Where (?-sm)(?s).* resets.

                          – sln
                          Apr 26 '18 at 21:30








                        • 1





                          Can you also add for bash please?

                          – Pasupathi Rajamanickam
                          Dec 19 '18 at 2:12






                        • 2





                          @PasupathiRajamanickam Bash uses a POSIX regex engine, the . matches any char there (including line breaks). See this online Bash demo.

                          – Wiktor Stribiżew
                          Dec 19 '18 at 7:33










                        4




                        4





                        You should link to this excellent overview from your profile page or something (+1).

                        – Jan
                        Oct 15 '17 at 20:15





                        You should link to this excellent overview from your profile page or something (+1).

                        – Jan
                        Oct 15 '17 at 20:15




                        1




                        1





                        You may want to add this to the boost item: In the regex_constants namespace, flag_type_'s : perl = ECMAScript = JavaScript = JScript = ::boost::regbase::normal = 0 which defaults to Perl. Programmers will set a base flag definition #define MOD regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m for thier regex flags to reflect that. And the arbitor is always the inline modifiers. Where (?-sm)(?s).* resets.

                        – sln
                        Apr 26 '18 at 21:30







                        You may want to add this to the boost item: In the regex_constants namespace, flag_type_'s : perl = ECMAScript = JavaScript = JScript = ::boost::regbase::normal = 0 which defaults to Perl. Programmers will set a base flag definition #define MOD regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m for thier regex flags to reflect that. And the arbitor is always the inline modifiers. Where (?-sm)(?s).* resets.

                        – sln
                        Apr 26 '18 at 21:30






                        1




                        1





                        Can you also add for bash please?

                        – Pasupathi Rajamanickam
                        Dec 19 '18 at 2:12





                        Can you also add for bash please?

                        – Pasupathi Rajamanickam
                        Dec 19 '18 at 2:12




                        2




                        2





                        @PasupathiRajamanickam Bash uses a POSIX regex engine, the . matches any char there (including line breaks). See this online Bash demo.

                        – Wiktor Stribiżew
                        Dec 19 '18 at 7:33







                        @PasupathiRajamanickam Bash uses a POSIX regex engine, the . matches any char there (including line breaks). See this online Bash demo.

                        – Wiktor Stribiżew
                        Dec 19 '18 at 7:33













                        31














                        In JavaScript, use /[Ss]*<Foobar>/. Source






                        share|improve this answer





















                        • 2





                          From that link: "JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [sS] to match any character." Instead of the . use [sS] (match spaces and non-spaces) instead.

                          – Allen
                          May 9 '13 at 15:34
















                        31














                        In JavaScript, use /[Ss]*<Foobar>/. Source






                        share|improve this answer





















                        • 2





                          From that link: "JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [sS] to match any character." Instead of the . use [sS] (match spaces and non-spaces) instead.

                          – Allen
                          May 9 '13 at 15:34














                        31












                        31








                        31







                        In JavaScript, use /[Ss]*<Foobar>/. Source






                        share|improve this answer















                        In JavaScript, use /[Ss]*<Foobar>/. Source







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 23 '17 at 1:15









                        Nathan Arthur

                        3,00032447




                        3,00032447










                        answered Jul 30 '11 at 13:03









                        Abbas ShahzadehAbbas Shahzadeh

                        31132




                        31132








                        • 2





                          From that link: "JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [sS] to match any character." Instead of the . use [sS] (match spaces and non-spaces) instead.

                          – Allen
                          May 9 '13 at 15:34














                        • 2





                          From that link: "JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [sS] to match any character." Instead of the . use [sS] (match spaces and non-spaces) instead.

                          – Allen
                          May 9 '13 at 15:34








                        2




                        2





                        From that link: "JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [sS] to match any character." Instead of the . use [sS] (match spaces and non-spaces) instead.

                        – Allen
                        May 9 '13 at 15:34





                        From that link: "JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [sS] to match any character." Instead of the . use [sS] (match spaces and non-spaces) instead.

                        – Allen
                        May 9 '13 at 15:34











                        27














                        ([sS]*)<FooBar>



                        The dot matches all except newlines (rn). So use sS, which will match ALL characters.






                        share|improve this answer
























                        • This solve the problem if you are using the Objective-C [text rangeOfString:regEx options:NSRegularExpressionSearch]. Thanks!

                          – J. Costa
                          Aug 24 '12 at 22:29













                        • This works in intelliJ's find&replace regex, thanks.

                          – barclay
                          Sep 16 '15 at 22:14











                        • This works. But it needs to be the first occurrence of <FooBar>

                          – Ozkan
                          Sep 26 '17 at 14:16
















                        27














                        ([sS]*)<FooBar>



                        The dot matches all except newlines (rn). So use sS, which will match ALL characters.






                        share|improve this answer
























                        • This solve the problem if you are using the Objective-C [text rangeOfString:regEx options:NSRegularExpressionSearch]. Thanks!

                          – J. Costa
                          Aug 24 '12 at 22:29













                        • This works in intelliJ's find&replace regex, thanks.

                          – barclay
                          Sep 16 '15 at 22:14











                        • This works. But it needs to be the first occurrence of <FooBar>

                          – Ozkan
                          Sep 26 '17 at 14:16














                        27












                        27








                        27







                        ([sS]*)<FooBar>



                        The dot matches all except newlines (rn). So use sS, which will match ALL characters.






                        share|improve this answer













                        ([sS]*)<FooBar>



                        The dot matches all except newlines (rn). So use sS, which will match ALL characters.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 19 '12 at 17:59









                        samwizesamwize

                        14.7k882140




                        14.7k882140













                        • This solve the problem if you are using the Objective-C [text rangeOfString:regEx options:NSRegularExpressionSearch]. Thanks!

                          – J. Costa
                          Aug 24 '12 at 22:29













                        • This works in intelliJ's find&replace regex, thanks.

                          – barclay
                          Sep 16 '15 at 22:14











                        • This works. But it needs to be the first occurrence of <FooBar>

                          – Ozkan
                          Sep 26 '17 at 14:16



















                        • This solve the problem if you are using the Objective-C [text rangeOfString:regEx options:NSRegularExpressionSearch]. Thanks!

                          – J. Costa
                          Aug 24 '12 at 22:29













                        • This works in intelliJ's find&replace regex, thanks.

                          – barclay
                          Sep 16 '15 at 22:14











                        • This works. But it needs to be the first occurrence of <FooBar>

                          – Ozkan
                          Sep 26 '17 at 14:16

















                        This solve the problem if you are using the Objective-C [text rangeOfString:regEx options:NSRegularExpressionSearch]. Thanks!

                        – J. Costa
                        Aug 24 '12 at 22:29







                        This solve the problem if you are using the Objective-C [text rangeOfString:regEx options:NSRegularExpressionSearch]. Thanks!

                        – J. Costa
                        Aug 24 '12 at 22:29















                        This works in intelliJ's find&replace regex, thanks.

                        – barclay
                        Sep 16 '15 at 22:14





                        This works in intelliJ's find&replace regex, thanks.

                        – barclay
                        Sep 16 '15 at 22:14













                        This works. But it needs to be the first occurrence of <FooBar>

                        – Ozkan
                        Sep 26 '17 at 14:16





                        This works. But it needs to be the first occurrence of <FooBar>

                        – Ozkan
                        Sep 26 '17 at 14:16











                        18














                        In Ruby ruby you can use the 'm' option (multiline):



                        /YOUR_REGEXP/m


                        See the Regexp documentation on ruby-doc.org for more information.






                        share|improve this answer






























                          18














                          In Ruby ruby you can use the 'm' option (multiline):



                          /YOUR_REGEXP/m


                          See the Regexp documentation on ruby-doc.org for more information.






                          share|improve this answer




























                            18












                            18








                            18







                            In Ruby ruby you can use the 'm' option (multiline):



                            /YOUR_REGEXP/m


                            See the Regexp documentation on ruby-doc.org for more information.






                            share|improve this answer















                            In Ruby ruby you can use the 'm' option (multiline):



                            /YOUR_REGEXP/m


                            See the Regexp documentation on ruby-doc.org for more information.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 31 '17 at 7:54









                            Wiktor Stribiżew

                            320k16140222




                            320k16140222










                            answered Aug 3 '12 at 7:52









                            vibaihervibaiher

                            38228




                            38228























                                10














                                we can also use



                                (.*?n)*?


                                to match everything including newline without greedy



                                This will make the new line optional



                                (.*?|n)*?





                                share|improve this answer




























                                  10














                                  we can also use



                                  (.*?n)*?


                                  to match everything including newline without greedy



                                  This will make the new line optional



                                  (.*?|n)*?





                                  share|improve this answer


























                                    10












                                    10








                                    10







                                    we can also use



                                    (.*?n)*?


                                    to match everything including newline without greedy



                                    This will make the new line optional



                                    (.*?|n)*?





                                    share|improve this answer













                                    we can also use



                                    (.*?n)*?


                                    to match everything including newline without greedy



                                    This will make the new line optional



                                    (.*?|n)*?






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 6 '18 at 7:48









                                    RAN_0915RAN_0915

                                    861216




                                    861216























                                        8














                                        "." normally doesn't match line-breaks. Most regex engines allows you to add the S-flag (also called DOTALL and SINGLELINE) to make "." also match newlines.
                                        If that fails, you could do something like [Ss].






                                        share|improve this answer






























                                          8














                                          "." normally doesn't match line-breaks. Most regex engines allows you to add the S-flag (also called DOTALL and SINGLELINE) to make "." also match newlines.
                                          If that fails, you could do something like [Ss].






                                          share|improve this answer




























                                            8












                                            8








                                            8







                                            "." normally doesn't match line-breaks. Most regex engines allows you to add the S-flag (also called DOTALL and SINGLELINE) to make "." also match newlines.
                                            If that fails, you could do something like [Ss].






                                            share|improve this answer















                                            "." normally doesn't match line-breaks. Most regex engines allows you to add the S-flag (also called DOTALL and SINGLELINE) to make "." also match newlines.
                                            If that fails, you could do something like [Ss].







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Apr 25 '09 at 21:09

























                                            answered Oct 1 '08 at 18:52









                                            Markus JarderotMarkus Jarderot

                                            66.8k12112120




                                            66.8k12112120























                                                7














                                                For Eclipse worked following expression:




                                                Foo



                                                jadajada Bar"




                                                Regular-Expression:



                                                Foo[Ss]{1,10}.*Bar*





                                                share|improve this answer






























                                                  7














                                                  For Eclipse worked following expression:




                                                  Foo



                                                  jadajada Bar"




                                                  Regular-Expression:



                                                  Foo[Ss]{1,10}.*Bar*





                                                  share|improve this answer




























                                                    7












                                                    7








                                                    7







                                                    For Eclipse worked following expression:




                                                    Foo



                                                    jadajada Bar"




                                                    Regular-Expression:



                                                    Foo[Ss]{1,10}.*Bar*





                                                    share|improve this answer















                                                    For Eclipse worked following expression:




                                                    Foo



                                                    jadajada Bar"




                                                    Regular-Expression:



                                                    Foo[Ss]{1,10}.*Bar*






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Jan 3 '13 at 11:52









                                                    devOp

                                                    2,91411232




                                                    2,91411232










                                                    answered Jan 3 '13 at 11:32









                                                    GordonGordon

                                                    7111




                                                    7111























                                                        5














                                                        /(.*)<FooBar>/s


                                                        the s causes Dot (.) to match carriage returns






                                                        share|improve this answer
























                                                        • Seems like this is invalid (Chrome): text.match(/a/s) SyntaxError: Invalid flags supplied to RegExp constructor 's'

                                                          – Allen
                                                          May 9 '13 at 15:31













                                                        • Because it is unsupported in JavaScript RegEx engines. The s flags exists in PCRE, the most complete engine (available in Perl and PHP). PCRE has 10 flags (and a lot of other features) while JavaScript has only 3 flags (gmi).

                                                          – Morgan Touverey Quilling
                                                          Apr 20 '16 at 18:51
















                                                        5














                                                        /(.*)<FooBar>/s


                                                        the s causes Dot (.) to match carriage returns






                                                        share|improve this answer
























                                                        • Seems like this is invalid (Chrome): text.match(/a/s) SyntaxError: Invalid flags supplied to RegExp constructor 's'

                                                          – Allen
                                                          May 9 '13 at 15:31













                                                        • Because it is unsupported in JavaScript RegEx engines. The s flags exists in PCRE, the most complete engine (available in Perl and PHP). PCRE has 10 flags (and a lot of other features) while JavaScript has only 3 flags (gmi).

                                                          – Morgan Touverey Quilling
                                                          Apr 20 '16 at 18:51














                                                        5












                                                        5








                                                        5







                                                        /(.*)<FooBar>/s


                                                        the s causes Dot (.) to match carriage returns






                                                        share|improve this answer













                                                        /(.*)<FooBar>/s


                                                        the s causes Dot (.) to match carriage returns







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Oct 1 '08 at 18:54









                                                        BillBill

                                                        2,32882429




                                                        2,32882429













                                                        • Seems like this is invalid (Chrome): text.match(/a/s) SyntaxError: Invalid flags supplied to RegExp constructor 's'

                                                          – Allen
                                                          May 9 '13 at 15:31













                                                        • Because it is unsupported in JavaScript RegEx engines. The s flags exists in PCRE, the most complete engine (available in Perl and PHP). PCRE has 10 flags (and a lot of other features) while JavaScript has only 3 flags (gmi).

                                                          – Morgan Touverey Quilling
                                                          Apr 20 '16 at 18:51



















                                                        • Seems like this is invalid (Chrome): text.match(/a/s) SyntaxError: Invalid flags supplied to RegExp constructor 's'

                                                          – Allen
                                                          May 9 '13 at 15:31













                                                        • Because it is unsupported in JavaScript RegEx engines. The s flags exists in PCRE, the most complete engine (available in Perl and PHP). PCRE has 10 flags (and a lot of other features) while JavaScript has only 3 flags (gmi).

                                                          – Morgan Touverey Quilling
                                                          Apr 20 '16 at 18:51

















                                                        Seems like this is invalid (Chrome): text.match(/a/s) SyntaxError: Invalid flags supplied to RegExp constructor 's'

                                                        – Allen
                                                        May 9 '13 at 15:31







                                                        Seems like this is invalid (Chrome): text.match(/a/s) SyntaxError: Invalid flags supplied to RegExp constructor 's'

                                                        – Allen
                                                        May 9 '13 at 15:31















                                                        Because it is unsupported in JavaScript RegEx engines. The s flags exists in PCRE, the most complete engine (available in Perl and PHP). PCRE has 10 flags (and a lot of other features) while JavaScript has only 3 flags (gmi).

                                                        – Morgan Touverey Quilling
                                                        Apr 20 '16 at 18:51





                                                        Because it is unsupported in JavaScript RegEx engines. The s flags exists in PCRE, the most complete engine (available in Perl and PHP). PCRE has 10 flags (and a lot of other features) while JavaScript has only 3 flags (gmi).

                                                        – Morgan Touverey Quilling
                                                        Apr 20 '16 at 18:51











                                                        4














                                                        In java based regular expression you can use [sS]






                                                        share|improve this answer





















                                                        • 1





                                                          Shouldn't those be backslashes?

                                                          – Paul Draper
                                                          Oct 19 '13 at 6:48











                                                        • They go at the end of the Regular Expression, not within in. Example: /blah/s

                                                          – RandomInsano
                                                          Dec 21 '13 at 20:12











                                                        • I guess you mean JavaScript, not Java? Since you can just add the s flag to the pattern in Java and JavaScript doesn't have the s flag.

                                                          – 3limin4t0r
                                                          Sep 25 '18 at 17:47


















                                                        4














                                                        In java based regular expression you can use [sS]






                                                        share|improve this answer





















                                                        • 1





                                                          Shouldn't those be backslashes?

                                                          – Paul Draper
                                                          Oct 19 '13 at 6:48











                                                        • They go at the end of the Regular Expression, not within in. Example: /blah/s

                                                          – RandomInsano
                                                          Dec 21 '13 at 20:12











                                                        • I guess you mean JavaScript, not Java? Since you can just add the s flag to the pattern in Java and JavaScript doesn't have the s flag.

                                                          – 3limin4t0r
                                                          Sep 25 '18 at 17:47
















                                                        4












                                                        4








                                                        4







                                                        In java based regular expression you can use [sS]






                                                        share|improve this answer















                                                        In java based regular expression you can use [sS]







                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited May 25 '18 at 19:01









                                                        revo

                                                        33.3k135085




                                                        33.3k135085










                                                        answered Jun 3 '13 at 6:22









                                                        KamahireKamahire

                                                        1,47721646




                                                        1,47721646








                                                        • 1





                                                          Shouldn't those be backslashes?

                                                          – Paul Draper
                                                          Oct 19 '13 at 6:48











                                                        • They go at the end of the Regular Expression, not within in. Example: /blah/s

                                                          – RandomInsano
                                                          Dec 21 '13 at 20:12











                                                        • I guess you mean JavaScript, not Java? Since you can just add the s flag to the pattern in Java and JavaScript doesn't have the s flag.

                                                          – 3limin4t0r
                                                          Sep 25 '18 at 17:47
















                                                        • 1





                                                          Shouldn't those be backslashes?

                                                          – Paul Draper
                                                          Oct 19 '13 at 6:48











                                                        • They go at the end of the Regular Expression, not within in. Example: /blah/s

                                                          – RandomInsano
                                                          Dec 21 '13 at 20:12











                                                        • I guess you mean JavaScript, not Java? Since you can just add the s flag to the pattern in Java and JavaScript doesn't have the s flag.

                                                          – 3limin4t0r
                                                          Sep 25 '18 at 17:47










                                                        1




                                                        1





                                                        Shouldn't those be backslashes?

                                                        – Paul Draper
                                                        Oct 19 '13 at 6:48





                                                        Shouldn't those be backslashes?

                                                        – Paul Draper
                                                        Oct 19 '13 at 6:48













                                                        They go at the end of the Regular Expression, not within in. Example: /blah/s

                                                        – RandomInsano
                                                        Dec 21 '13 at 20:12





                                                        They go at the end of the Regular Expression, not within in. Example: /blah/s

                                                        – RandomInsano
                                                        Dec 21 '13 at 20:12













                                                        I guess you mean JavaScript, not Java? Since you can just add the s flag to the pattern in Java and JavaScript doesn't have the s flag.

                                                        – 3limin4t0r
                                                        Sep 25 '18 at 17:47







                                                        I guess you mean JavaScript, not Java? Since you can just add the s flag to the pattern in Java and JavaScript doesn't have the s flag.

                                                        – 3limin4t0r
                                                        Sep 25 '18 at 17:47













                                                        3














                                                        Note that (.|n)* can be less efficient than (for example) [sS]* (if your language's regexes support such escapes) and than finding how to specify the modifier that makes . also match newlines. Or you can go with POSIXy alternatives like [[:space:][:^space:]]*.






                                                        share|improve this answer




























                                                          3














                                                          Note that (.|n)* can be less efficient than (for example) [sS]* (if your language's regexes support such escapes) and than finding how to specify the modifier that makes . also match newlines. Or you can go with POSIXy alternatives like [[:space:][:^space:]]*.






                                                          share|improve this answer


























                                                            3












                                                            3








                                                            3







                                                            Note that (.|n)* can be less efficient than (for example) [sS]* (if your language's regexes support such escapes) and than finding how to specify the modifier that makes . also match newlines. Or you can go with POSIXy alternatives like [[:space:][:^space:]]*.






                                                            share|improve this answer













                                                            Note that (.|n)* can be less efficient than (for example) [sS]* (if your language's regexes support such escapes) and than finding how to specify the modifier that makes . also match newlines. Or you can go with POSIXy alternatives like [[:space:][:^space:]]*.







                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Oct 2 '08 at 3:31









                                                            tyetye

                                                            1,072911




                                                            1,072911























                                                                3














                                                                Use RegexOptions.Singleline, it changes the meaning of . to include newlines



                                                                Regex.Replace(content, searchText, replaceText, RegexOptions.Singleline);






                                                                share|improve this answer




























                                                                  3














                                                                  Use RegexOptions.Singleline, it changes the meaning of . to include newlines



                                                                  Regex.Replace(content, searchText, replaceText, RegexOptions.Singleline);






                                                                  share|improve this answer


























                                                                    3












                                                                    3








                                                                    3







                                                                    Use RegexOptions.Singleline, it changes the meaning of . to include newlines



                                                                    Regex.Replace(content, searchText, replaceText, RegexOptions.Singleline);






                                                                    share|improve this answer













                                                                    Use RegexOptions.Singleline, it changes the meaning of . to include newlines



                                                                    Regex.Replace(content, searchText, replaceText, RegexOptions.Singleline);







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Apr 13 '10 at 0:42









                                                                    shmallshmall

                                                                    311




                                                                    311























                                                                        2














                                                                        Solution:



                                                                        Use pattern modifier sU will get the desired matching in PHP.



                                                                        example:



                                                                        preg_match('/(.*)/sU',$content,$match);


                                                                        Source:



                                                                        http://dreamluverz.com/developers-tools/regex-match-all-including-new-line
                                                                        http://php.net/manual/en/reference.pcre.pattern.modifiers.php






                                                                        share|improve this answer




























                                                                          2














                                                                          Solution:



                                                                          Use pattern modifier sU will get the desired matching in PHP.



                                                                          example:



                                                                          preg_match('/(.*)/sU',$content,$match);


                                                                          Source:



                                                                          http://dreamluverz.com/developers-tools/regex-match-all-including-new-line
                                                                          http://php.net/manual/en/reference.pcre.pattern.modifiers.php






                                                                          share|improve this answer


























                                                                            2












                                                                            2








                                                                            2







                                                                            Solution:



                                                                            Use pattern modifier sU will get the desired matching in PHP.



                                                                            example:



                                                                            preg_match('/(.*)/sU',$content,$match);


                                                                            Source:



                                                                            http://dreamluverz.com/developers-tools/regex-match-all-including-new-line
                                                                            http://php.net/manual/en/reference.pcre.pattern.modifiers.php






                                                                            share|improve this answer













                                                                            Solution:



                                                                            Use pattern modifier sU will get the desired matching in PHP.



                                                                            example:



                                                                            preg_match('/(.*)/sU',$content,$match);


                                                                            Source:



                                                                            http://dreamluverz.com/developers-tools/regex-match-all-including-new-line
                                                                            http://php.net/manual/en/reference.pcre.pattern.modifiers.php







                                                                            share|improve this answer












                                                                            share|improve this answer



                                                                            share|improve this answer










                                                                            answered Apr 4 '12 at 11:00









                                                                            Sian Lerk LauSian Lerk Lau

                                                                            1088




                                                                            1088























                                                                                1














                                                                                In the context of use within languages, regular expressions act on strings, not lines. So you should be able to use the regex normally, assuming that the input string has multiple lines.



                                                                                In this case, the given regex will match the entire string, since "<FooBar>" is present. Depending on the specifics of the regex implementation, the $1 value (obtained from the "(.*)") will either be "fghij" or "abcdenfghij". As others have said, some implementations allow you to control whether the "." will match the newline, giving you the choice.



                                                                                Line-based regular expression use is usually for command line things like egrep.






                                                                                share|improve this answer






























                                                                                  1














                                                                                  In the context of use within languages, regular expressions act on strings, not lines. So you should be able to use the regex normally, assuming that the input string has multiple lines.



                                                                                  In this case, the given regex will match the entire string, since "<FooBar>" is present. Depending on the specifics of the regex implementation, the $1 value (obtained from the "(.*)") will either be "fghij" or "abcdenfghij". As others have said, some implementations allow you to control whether the "." will match the newline, giving you the choice.



                                                                                  Line-based regular expression use is usually for command line things like egrep.






                                                                                  share|improve this answer




























                                                                                    1












                                                                                    1








                                                                                    1







                                                                                    In the context of use within languages, regular expressions act on strings, not lines. So you should be able to use the regex normally, assuming that the input string has multiple lines.



                                                                                    In this case, the given regex will match the entire string, since "<FooBar>" is present. Depending on the specifics of the regex implementation, the $1 value (obtained from the "(.*)") will either be "fghij" or "abcdenfghij". As others have said, some implementations allow you to control whether the "." will match the newline, giving you the choice.



                                                                                    Line-based regular expression use is usually for command line things like egrep.






                                                                                    share|improve this answer















                                                                                    In the context of use within languages, regular expressions act on strings, not lines. So you should be able to use the regex normally, assuming that the input string has multiple lines.



                                                                                    In this case, the given regex will match the entire string, since "<FooBar>" is present. Depending on the specifics of the regex implementation, the $1 value (obtained from the "(.*)") will either be "fghij" or "abcdenfghij". As others have said, some implementations allow you to control whether the "." will match the newline, giving you the choice.



                                                                                    Line-based regular expression use is usually for command line things like egrep.







                                                                                    share|improve this answer














                                                                                    share|improve this answer



                                                                                    share|improve this answer








                                                                                    edited Oct 1 '08 at 18:54

























                                                                                    answered Oct 1 '08 at 18:49









                                                                                    nsayernsayer

                                                                                    13.5k22645




                                                                                    13.5k22645























                                                                                        1














                                                                                        I had the same problem and solved it in probably not the best way but it works. I replaced all line breaks before I did my real match:



                                                                                        mystring= Regex.Replace(mystring, "rn", "")


                                                                                        I am manipulating HTML so line breaks don't really matter to me in this case.



                                                                                        I tried all of the suggestions above with no luck, I am using .Net 3.5 FYI






                                                                                        share|improve this answer
























                                                                                        • I am using .NET too and (s|S) seems to do the trick for me!

                                                                                          – Vamshi Krishna
                                                                                          May 18 '18 at 7:26













                                                                                        • @VamshiKrishna In .NET, use (?s) to make . match any chars. Do not use (s|S) that will slow down performance.

                                                                                          – Wiktor Stribiżew
                                                                                          Sep 14 '18 at 20:35
















                                                                                        1














                                                                                        I had the same problem and solved it in probably not the best way but it works. I replaced all line breaks before I did my real match:



                                                                                        mystring= Regex.Replace(mystring, "rn", "")


                                                                                        I am manipulating HTML so line breaks don't really matter to me in this case.



                                                                                        I tried all of the suggestions above with no luck, I am using .Net 3.5 FYI






                                                                                        share|improve this answer
























                                                                                        • I am using .NET too and (s|S) seems to do the trick for me!

                                                                                          – Vamshi Krishna
                                                                                          May 18 '18 at 7:26













                                                                                        • @VamshiKrishna In .NET, use (?s) to make . match any chars. Do not use (s|S) that will slow down performance.

                                                                                          – Wiktor Stribiżew
                                                                                          Sep 14 '18 at 20:35














                                                                                        1












                                                                                        1








                                                                                        1







                                                                                        I had the same problem and solved it in probably not the best way but it works. I replaced all line breaks before I did my real match:



                                                                                        mystring= Regex.Replace(mystring, "rn", "")


                                                                                        I am manipulating HTML so line breaks don't really matter to me in this case.



                                                                                        I tried all of the suggestions above with no luck, I am using .Net 3.5 FYI






                                                                                        share|improve this answer













                                                                                        I had the same problem and solved it in probably not the best way but it works. I replaced all line breaks before I did my real match:



                                                                                        mystring= Regex.Replace(mystring, "rn", "")


                                                                                        I am manipulating HTML so line breaks don't really matter to me in this case.



                                                                                        I tried all of the suggestions above with no luck, I am using .Net 3.5 FYI







                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Mar 26 '09 at 14:57









                                                                                        SleeSlee

                                                                                        11.4k41129232




                                                                                        11.4k41129232













                                                                                        • I am using .NET too and (s|S) seems to do the trick for me!

                                                                                          – Vamshi Krishna
                                                                                          May 18 '18 at 7:26













                                                                                        • @VamshiKrishna In .NET, use (?s) to make . match any chars. Do not use (s|S) that will slow down performance.

                                                                                          – Wiktor Stribiżew
                                                                                          Sep 14 '18 at 20:35



















                                                                                        • I am using .NET too and (s|S) seems to do the trick for me!

                                                                                          – Vamshi Krishna
                                                                                          May 18 '18 at 7:26













                                                                                        • @VamshiKrishna In .NET, use (?s) to make . match any chars. Do not use (s|S) that will slow down performance.

                                                                                          – Wiktor Stribiżew
                                                                                          Sep 14 '18 at 20:35

















                                                                                        I am using .NET too and (s|S) seems to do the trick for me!

                                                                                        – Vamshi Krishna
                                                                                        May 18 '18 at 7:26







                                                                                        I am using .NET too and (s|S) seems to do the trick for me!

                                                                                        – Vamshi Krishna
                                                                                        May 18 '18 at 7:26















                                                                                        @VamshiKrishna In .NET, use (?s) to make . match any chars. Do not use (s|S) that will slow down performance.

                                                                                        – Wiktor Stribiżew
                                                                                        Sep 14 '18 at 20:35





                                                                                        @VamshiKrishna In .NET, use (?s) to make . match any chars. Do not use (s|S) that will slow down performance.

                                                                                        – Wiktor Stribiżew
                                                                                        Sep 14 '18 at 20:35











                                                                                        0














                                                                                        generally . doesn't match newlines, so try ((.|n)*)<foobar>






                                                                                        share|improve this answer



















                                                                                        • 1





                                                                                          No, don't do that. If you need to match anything including line separators, use the DOTALL (a.k.a. /s or SingleLine) modifier. Not only does the (.|n) hack make the regex less efficient, it's not even correct. At the very least, it should match r (carriage return) as well as n (linefeed). There are other line separator characters, too, albeit rarely used. But if you use the DOTALL flag, you don't have to worry about them.

                                                                                          – Alan Moore
                                                                                          Apr 26 '09 at 3:17






                                                                                        • 1





                                                                                          R is the platform-independent match for newlines in Eclipse.

                                                                                          – opyate
                                                                                          Nov 30 '09 at 11:13











                                                                                        • @opyate You should post this as an answer as this little gem is incredibly useful.

                                                                                          – jeckhart
                                                                                          Oct 15 '12 at 21:29











                                                                                        • You could try this instead. It won't match the inner brackets and also consider the optionalr.: ((?:.|r?n)*)<foobar>

                                                                                          – ssc-hrep3
                                                                                          Nov 29 '16 at 9:52
















                                                                                        0














                                                                                        generally . doesn't match newlines, so try ((.|n)*)<foobar>






                                                                                        share|improve this answer



















                                                                                        • 1





                                                                                          No, don't do that. If you need to match anything including line separators, use the DOTALL (a.k.a. /s or SingleLine) modifier. Not only does the (.|n) hack make the regex less efficient, it's not even correct. At the very least, it should match r (carriage return) as well as n (linefeed). There are other line separator characters, too, albeit rarely used. But if you use the DOTALL flag, you don't have to worry about them.

                                                                                          – Alan Moore
                                                                                          Apr 26 '09 at 3:17






                                                                                        • 1





                                                                                          R is the platform-independent match for newlines in Eclipse.

                                                                                          – opyate
                                                                                          Nov 30 '09 at 11:13











                                                                                        • @opyate You should post this as an answer as this little gem is incredibly useful.

                                                                                          – jeckhart
                                                                                          Oct 15 '12 at 21:29











                                                                                        • You could try this instead. It won't match the inner brackets and also consider the optionalr.: ((?:.|r?n)*)<foobar>

                                                                                          – ssc-hrep3
                                                                                          Nov 29 '16 at 9:52














                                                                                        0












                                                                                        0








                                                                                        0







                                                                                        generally . doesn't match newlines, so try ((.|n)*)<foobar>






                                                                                        share|improve this answer













                                                                                        generally . doesn't match newlines, so try ((.|n)*)<foobar>







                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Oct 1 '08 at 18:52









                                                                                        tloachtloach

                                                                                        7,53112943




                                                                                        7,53112943








                                                                                        • 1





                                                                                          No, don't do that. If you need to match anything including line separators, use the DOTALL (a.k.a. /s or SingleLine) modifier. Not only does the (.|n) hack make the regex less efficient, it's not even correct. At the very least, it should match r (carriage return) as well as n (linefeed). There are other line separator characters, too, albeit rarely used. But if you use the DOTALL flag, you don't have to worry about them.

                                                                                          – Alan Moore
                                                                                          Apr 26 '09 at 3:17






                                                                                        • 1





                                                                                          R is the platform-independent match for newlines in Eclipse.

                                                                                          – opyate
                                                                                          Nov 30 '09 at 11:13











                                                                                        • @opyate You should post this as an answer as this little gem is incredibly useful.

                                                                                          – jeckhart
                                                                                          Oct 15 '12 at 21:29











                                                                                        • You could try this instead. It won't match the inner brackets and also consider the optionalr.: ((?:.|r?n)*)<foobar>

                                                                                          – ssc-hrep3
                                                                                          Nov 29 '16 at 9:52














                                                                                        • 1





                                                                                          No, don't do that. If you need to match anything including line separators, use the DOTALL (a.k.a. /s or SingleLine) modifier. Not only does the (.|n) hack make the regex less efficient, it's not even correct. At the very least, it should match r (carriage return) as well as n (linefeed). There are other line separator characters, too, albeit rarely used. But if you use the DOTALL flag, you don't have to worry about them.

                                                                                          – Alan Moore
                                                                                          Apr 26 '09 at 3:17






                                                                                        • 1





                                                                                          R is the platform-independent match for newlines in Eclipse.

                                                                                          – opyate
                                                                                          Nov 30 '09 at 11:13











                                                                                        • @opyate You should post this as an answer as this little gem is incredibly useful.

                                                                                          – jeckhart
                                                                                          Oct 15 '12 at 21:29











                                                                                        • You could try this instead. It won't match the inner brackets and also consider the optionalr.: ((?:.|r?n)*)<foobar>

                                                                                          – ssc-hrep3
                                                                                          Nov 29 '16 at 9:52








                                                                                        1




                                                                                        1





                                                                                        No, don't do that. If you need to match anything including line separators, use the DOTALL (a.k.a. /s or SingleLine) modifier. Not only does the (.|n) hack make the regex less efficient, it's not even correct. At the very least, it should match r (carriage return) as well as n (linefeed). There are other line separator characters, too, albeit rarely used. But if you use the DOTALL flag, you don't have to worry about them.

                                                                                        – Alan Moore
                                                                                        Apr 26 '09 at 3:17





                                                                                        No, don't do that. If you need to match anything including line separators, use the DOTALL (a.k.a. /s or SingleLine) modifier. Not only does the (.|n) hack make the regex less efficient, it's not even correct. At the very least, it should match r (carriage return) as well as n (linefeed). There are other line separator characters, too, albeit rarely used. But if you use the DOTALL flag, you don't have to worry about them.

                                                                                        – Alan Moore
                                                                                        Apr 26 '09 at 3:17




                                                                                        1




                                                                                        1





                                                                                        R is the platform-independent match for newlines in Eclipse.

                                                                                        – opyate
                                                                                        Nov 30 '09 at 11:13





                                                                                        R is the platform-independent match for newlines in Eclipse.

                                                                                        – opyate
                                                                                        Nov 30 '09 at 11:13













                                                                                        @opyate You should post this as an answer as this little gem is incredibly useful.

                                                                                        – jeckhart
                                                                                        Oct 15 '12 at 21:29





                                                                                        @opyate You should post this as an answer as this little gem is incredibly useful.

                                                                                        – jeckhart
                                                                                        Oct 15 '12 at 21:29













                                                                                        You could try this instead. It won't match the inner brackets and also consider the optionalr.: ((?:.|r?n)*)<foobar>

                                                                                        – ssc-hrep3
                                                                                        Nov 29 '16 at 9:52





                                                                                        You could try this instead. It won't match the inner brackets and also consider the optionalr.: ((?:.|r?n)*)<foobar>

                                                                                        – ssc-hrep3
                                                                                        Nov 29 '16 at 9:52











                                                                                        0














                                                                                        I wanted to match a particular if block in java



                                                                                           ...
                                                                                        ...
                                                                                        if(isTrue){
                                                                                        doAction();

                                                                                        }
                                                                                        ...
                                                                                        ...
                                                                                        }


                                                                                        If I use the regExp



                                                                                        if (isTrue(.|n)*}


                                                                                        it included the closing brace for the method block so I used



                                                                                        if (!isTrue([^}.]|n)*}


                                                                                        to exclude the closing brace from the wildcard match.






                                                                                        share|improve this answer




























                                                                                          0














                                                                                          I wanted to match a particular if block in java



                                                                                             ...
                                                                                          ...
                                                                                          if(isTrue){
                                                                                          doAction();

                                                                                          }
                                                                                          ...
                                                                                          ...
                                                                                          }


                                                                                          If I use the regExp



                                                                                          if (isTrue(.|n)*}


                                                                                          it included the closing brace for the method block so I used



                                                                                          if (!isTrue([^}.]|n)*}


                                                                                          to exclude the closing brace from the wildcard match.






                                                                                          share|improve this answer


























                                                                                            0












                                                                                            0








                                                                                            0







                                                                                            I wanted to match a particular if block in java



                                                                                               ...
                                                                                            ...
                                                                                            if(isTrue){
                                                                                            doAction();

                                                                                            }
                                                                                            ...
                                                                                            ...
                                                                                            }


                                                                                            If I use the regExp



                                                                                            if (isTrue(.|n)*}


                                                                                            it included the closing brace for the method block so I used



                                                                                            if (!isTrue([^}.]|n)*}


                                                                                            to exclude the closing brace from the wildcard match.






                                                                                            share|improve this answer













                                                                                            I wanted to match a particular if block in java



                                                                                               ...
                                                                                            ...
                                                                                            if(isTrue){
                                                                                            doAction();

                                                                                            }
                                                                                            ...
                                                                                            ...
                                                                                            }


                                                                                            If I use the regExp



                                                                                            if (isTrue(.|n)*}


                                                                                            it included the closing brace for the method block so I used



                                                                                            if (!isTrue([^}.]|n)*}


                                                                                            to exclude the closing brace from the wildcard match.







                                                                                            share|improve this answer












                                                                                            share|improve this answer



                                                                                            share|improve this answer










                                                                                            answered Jan 18 '11 at 9:31









                                                                                            SpangenSpangen

                                                                                            1,89031824




                                                                                            1,89031824























                                                                                                0














                                                                                                Often we have to modify a substring with a few keywords spread across lines preceding the substring. Consider an xml element:



                                                                                                <TASK>
                                                                                                <UID>21</UID>
                                                                                                <Name>Architectural design</Name>
                                                                                                <PercentComplete>81</PercentComplete>
                                                                                                </TASK>


                                                                                                Suppose we want to modify the 81, to some other value, say 40. First identify .UID.21..UID., then skip all characters including n till .PercentCompleted.. The regular expression pattern and the replace specification are:



                                                                                                String hw = new String("<TASK>n  <UID>21</UID>n  <Name>Architectural design</Name>n  <PercentComplete>81</PercentComplete>n</TASK>");
                                                                                                String pattern = new String ("(<UID>21</UID>)((.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                                                                                                String replaceSpec = new String ("$1$2$440$6");
                                                                                                //note that the group (<PercentComplete>) is $4 and the group ((.|n)*?) is $2.

                                                                                                String iw = hw.replaceFirst(pattern, replaceSpec);
                                                                                                System.out.println(iw);

                                                                                                <TASK>
                                                                                                <UID>21</UID>
                                                                                                <Name>Architectural design</Name>
                                                                                                <PercentComplete>40</PercentComplete>
                                                                                                </TASK>


                                                                                                The subgroup (.|n) is probably the missing group $3. If we make it non-capturing by (?:.|n) then the $3 is (<PercentComplete>). So the pattern and replaceSpec can also be:



                                                                                                pattern = new String("(<UID>21</UID>)((?:.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                                                                                                replaceSpec = new String("$1$2$340$5")


                                                                                                and the replacement works correctly as before.






                                                                                                share|improve this answer






























                                                                                                  0














                                                                                                  Often we have to modify a substring with a few keywords spread across lines preceding the substring. Consider an xml element:



                                                                                                  <TASK>
                                                                                                  <UID>21</UID>
                                                                                                  <Name>Architectural design</Name>
                                                                                                  <PercentComplete>81</PercentComplete>
                                                                                                  </TASK>


                                                                                                  Suppose we want to modify the 81, to some other value, say 40. First identify .UID.21..UID., then skip all characters including n till .PercentCompleted.. The regular expression pattern and the replace specification are:



                                                                                                  String hw = new String("<TASK>n  <UID>21</UID>n  <Name>Architectural design</Name>n  <PercentComplete>81</PercentComplete>n</TASK>");
                                                                                                  String pattern = new String ("(<UID>21</UID>)((.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                                                                                                  String replaceSpec = new String ("$1$2$440$6");
                                                                                                  //note that the group (<PercentComplete>) is $4 and the group ((.|n)*?) is $2.

                                                                                                  String iw = hw.replaceFirst(pattern, replaceSpec);
                                                                                                  System.out.println(iw);

                                                                                                  <TASK>
                                                                                                  <UID>21</UID>
                                                                                                  <Name>Architectural design</Name>
                                                                                                  <PercentComplete>40</PercentComplete>
                                                                                                  </TASK>


                                                                                                  The subgroup (.|n) is probably the missing group $3. If we make it non-capturing by (?:.|n) then the $3 is (<PercentComplete>). So the pattern and replaceSpec can also be:



                                                                                                  pattern = new String("(<UID>21</UID>)((?:.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                                                                                                  replaceSpec = new String("$1$2$340$5")


                                                                                                  and the replacement works correctly as before.






                                                                                                  share|improve this answer




























                                                                                                    0












                                                                                                    0








                                                                                                    0







                                                                                                    Often we have to modify a substring with a few keywords spread across lines preceding the substring. Consider an xml element:



                                                                                                    <TASK>
                                                                                                    <UID>21</UID>
                                                                                                    <Name>Architectural design</Name>
                                                                                                    <PercentComplete>81</PercentComplete>
                                                                                                    </TASK>


                                                                                                    Suppose we want to modify the 81, to some other value, say 40. First identify .UID.21..UID., then skip all characters including n till .PercentCompleted.. The regular expression pattern and the replace specification are:



                                                                                                    String hw = new String("<TASK>n  <UID>21</UID>n  <Name>Architectural design</Name>n  <PercentComplete>81</PercentComplete>n</TASK>");
                                                                                                    String pattern = new String ("(<UID>21</UID>)((.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                                                                                                    String replaceSpec = new String ("$1$2$440$6");
                                                                                                    //note that the group (<PercentComplete>) is $4 and the group ((.|n)*?) is $2.

                                                                                                    String iw = hw.replaceFirst(pattern, replaceSpec);
                                                                                                    System.out.println(iw);

                                                                                                    <TASK>
                                                                                                    <UID>21</UID>
                                                                                                    <Name>Architectural design</Name>
                                                                                                    <PercentComplete>40</PercentComplete>
                                                                                                    </TASK>


                                                                                                    The subgroup (.|n) is probably the missing group $3. If we make it non-capturing by (?:.|n) then the $3 is (<PercentComplete>). So the pattern and replaceSpec can also be:



                                                                                                    pattern = new String("(<UID>21</UID>)((?:.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                                                                                                    replaceSpec = new String("$1$2$340$5")


                                                                                                    and the replacement works correctly as before.






                                                                                                    share|improve this answer















                                                                                                    Often we have to modify a substring with a few keywords spread across lines preceding the substring. Consider an xml element:



                                                                                                    <TASK>
                                                                                                    <UID>21</UID>
                                                                                                    <Name>Architectural design</Name>
                                                                                                    <PercentComplete>81</PercentComplete>
                                                                                                    </TASK>


                                                                                                    Suppose we want to modify the 81, to some other value, say 40. First identify .UID.21..UID., then skip all characters including n till .PercentCompleted.. The regular expression pattern and the replace specification are:



                                                                                                    String hw = new String("<TASK>n  <UID>21</UID>n  <Name>Architectural design</Name>n  <PercentComplete>81</PercentComplete>n</TASK>");
                                                                                                    String pattern = new String ("(<UID>21</UID>)((.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                                                                                                    String replaceSpec = new String ("$1$2$440$6");
                                                                                                    //note that the group (<PercentComplete>) is $4 and the group ((.|n)*?) is $2.

                                                                                                    String iw = hw.replaceFirst(pattern, replaceSpec);
                                                                                                    System.out.println(iw);

                                                                                                    <TASK>
                                                                                                    <UID>21</UID>
                                                                                                    <Name>Architectural design</Name>
                                                                                                    <PercentComplete>40</PercentComplete>
                                                                                                    </TASK>


                                                                                                    The subgroup (.|n) is probably the missing group $3. If we make it non-capturing by (?:.|n) then the $3 is (<PercentComplete>). So the pattern and replaceSpec can also be:



                                                                                                    pattern = new String("(<UID>21</UID>)((?:.|n)*?)(<PercentComplete>)(\d+)(</PercentComplete>)");
                                                                                                    replaceSpec = new String("$1$2$340$5")


                                                                                                    and the replacement works correctly as before.







                                                                                                    share|improve this answer














                                                                                                    share|improve this answer



                                                                                                    share|improve this answer








                                                                                                    edited Oct 26 '12 at 8:49









                                                                                                    deadly

                                                                                                    1,1431223




                                                                                                    1,1431223










                                                                                                    answered Apr 21 '12 at 20:05









                                                                                                    user1348737user1348737

                                                                                                    92




                                                                                                    92























                                                                                                        0














                                                                                                        In Javascript you can use [^]* to search for zero to infinite characters, including line breaks.






                                                                                                        $("#find_and_replace").click(function() {
                                                                                                        var text = $("#textarea").val();
                                                                                                        search_term = new RegExp("[^]*<Foobar>", "gi");;
                                                                                                        replace_term = "Replacement term";
                                                                                                        var new_text = text.replace(search_term, replace_term);
                                                                                                        $("#textarea").val(new_text);
                                                                                                        });

                                                                                                        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                                                                                                        <button id="find_and_replace">Find and replace</button>
                                                                                                        <br>
                                                                                                        <textarea ID="textarea">abcde
                                                                                                        fghij&lt;Foobar&gt;</textarea>








                                                                                                        share|improve this answer




























                                                                                                          0














                                                                                                          In Javascript you can use [^]* to search for zero to infinite characters, including line breaks.






                                                                                                          $("#find_and_replace").click(function() {
                                                                                                          var text = $("#textarea").val();
                                                                                                          search_term = new RegExp("[^]*<Foobar>", "gi");;
                                                                                                          replace_term = "Replacement term";
                                                                                                          var new_text = text.replace(search_term, replace_term);
                                                                                                          $("#textarea").val(new_text);
                                                                                                          });

                                                                                                          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                                                                                                          <button id="find_and_replace">Find and replace</button>
                                                                                                          <br>
                                                                                                          <textarea ID="textarea">abcde
                                                                                                          fghij&lt;Foobar&gt;</textarea>








                                                                                                          share|improve this answer


























                                                                                                            0












                                                                                                            0








                                                                                                            0







                                                                                                            In Javascript you can use [^]* to search for zero to infinite characters, including line breaks.






                                                                                                            $("#find_and_replace").click(function() {
                                                                                                            var text = $("#textarea").val();
                                                                                                            search_term = new RegExp("[^]*<Foobar>", "gi");;
                                                                                                            replace_term = "Replacement term";
                                                                                                            var new_text = text.replace(search_term, replace_term);
                                                                                                            $("#textarea").val(new_text);
                                                                                                            });

                                                                                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                                                                                                            <button id="find_and_replace">Find and replace</button>
                                                                                                            <br>
                                                                                                            <textarea ID="textarea">abcde
                                                                                                            fghij&lt;Foobar&gt;</textarea>








                                                                                                            share|improve this answer













                                                                                                            In Javascript you can use [^]* to search for zero to infinite characters, including line breaks.






                                                                                                            $("#find_and_replace").click(function() {
                                                                                                            var text = $("#textarea").val();
                                                                                                            search_term = new RegExp("[^]*<Foobar>", "gi");;
                                                                                                            replace_term = "Replacement term";
                                                                                                            var new_text = text.replace(search_term, replace_term);
                                                                                                            $("#textarea").val(new_text);
                                                                                                            });

                                                                                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                                                                                                            <button id="find_and_replace">Find and replace</button>
                                                                                                            <br>
                                                                                                            <textarea ID="textarea">abcde
                                                                                                            fghij&lt;Foobar&gt;</textarea>








                                                                                                            $("#find_and_replace").click(function() {
                                                                                                            var text = $("#textarea").val();
                                                                                                            search_term = new RegExp("[^]*<Foobar>", "gi");;
                                                                                                            replace_term = "Replacement term";
                                                                                                            var new_text = text.replace(search_term, replace_term);
                                                                                                            $("#textarea").val(new_text);
                                                                                                            });

                                                                                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                                                                                                            <button id="find_and_replace">Find and replace</button>
                                                                                                            <br>
                                                                                                            <textarea ID="textarea">abcde
                                                                                                            fghij&lt;Foobar&gt;</textarea>





                                                                                                            $("#find_and_replace").click(function() {
                                                                                                            var text = $("#textarea").val();
                                                                                                            search_term = new RegExp("[^]*<Foobar>", "gi");;
                                                                                                            replace_term = "Replacement term";
                                                                                                            var new_text = text.replace(search_term, replace_term);
                                                                                                            $("#textarea").val(new_text);
                                                                                                            });

                                                                                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                                                                                                            <button id="find_and_replace">Find and replace</button>
                                                                                                            <br>
                                                                                                            <textarea ID="textarea">abcde
                                                                                                            fghij&lt;Foobar&gt;</textarea>






                                                                                                            share|improve this answer












                                                                                                            share|improve this answer



                                                                                                            share|improve this answer










                                                                                                            answered yesterday









                                                                                                            Paul JonesPaul Jones

                                                                                                            329311




                                                                                                            329311






























                                                                                                                draft saved

                                                                                                                draft discarded




















































                                                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                                                But avoid



                                                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                                                To learn more, see our tips on writing great answers.




                                                                                                                draft saved


                                                                                                                draft discarded














                                                                                                                StackExchange.ready(
                                                                                                                function () {
                                                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f159118%2fhow-do-i-match-any-character-across-multiple-lines-in-a-regular-expression%23new-answer', 'question_page');
                                                                                                                }
                                                                                                                );

                                                                                                                Post as a guest















                                                                                                                Required, but never shown





















































                                                                                                                Required, but never shown














                                                                                                                Required, but never shown












                                                                                                                Required, but never shown







                                                                                                                Required, but never shown

































                                                                                                                Required, but never shown














                                                                                                                Required, but never shown












                                                                                                                Required, but never shown







                                                                                                                Required, but never shown







                                                                                                                Popular posts from this blog

                                                                                                                MongoDB - Not Authorized To Execute Command

                                                                                                                How to fix TextFormField cause rebuild widget in Flutter

                                                                                                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith