how to use regular expression to match strings followed by some keyword and multiple lines











up vote
-2
down vote

favorite












I have a string like this:



...
xxxx
xxx

keyword1 xxxxx
xxxx
xxxx
xxxxx keyword2 yyyy
xxx
xxxx

xxx
...


where the x and y's are just random chars. I need to match the first "keyword2 yyyy" that appears after keyword1, and there may be multiple lines after keyword1. How do I write the regular expression? Thanks!










share|improve this question




























    up vote
    -2
    down vote

    favorite












    I have a string like this:



    ...
    xxxx
    xxx

    keyword1 xxxxx
    xxxx
    xxxx
    xxxxx keyword2 yyyy
    xxx
    xxxx

    xxx
    ...


    where the x and y's are just random chars. I need to match the first "keyword2 yyyy" that appears after keyword1, and there may be multiple lines after keyword1. How do I write the regular expression? Thanks!










    share|improve this question


























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I have a string like this:



      ...
      xxxx
      xxx

      keyword1 xxxxx
      xxxx
      xxxx
      xxxxx keyword2 yyyy
      xxx
      xxxx

      xxx
      ...


      where the x and y's are just random chars. I need to match the first "keyword2 yyyy" that appears after keyword1, and there may be multiple lines after keyword1. How do I write the regular expression? Thanks!










      share|improve this question















      I have a string like this:



      ...
      xxxx
      xxx

      keyword1 xxxxx
      xxxx
      xxxx
      xxxxx keyword2 yyyy
      xxx
      xxxx

      xxx
      ...


      where the x and y's are just random chars. I need to match the first "keyword2 yyyy" that appears after keyword1, and there may be multiple lines after keyword1. How do I write the regular expression? Thanks!







      regex string pattern-matching matching






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 6:39

























      asked Nov 7 at 6:22









      user2577547

      185




      185
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          -1
          down vote



          accepted










          You can use this regex,



          ^(?s).*keyword1.*?(keyword2 yyyy).*$


          Explanation:




          • ^ --> start of string

          • (?s) --> Enables dot to match new lines

          • .* keyword1.*? --> Matches a string that contains keyword1 preceded and succeeded by any characters doing non-greedy match

          • (keyword2 yyyy) --> matches the string of your interest

          • .*$ --> followed by any characters and finally end of input


          Demo






          share|improve this answer





















          • Thank you Pushpesh! I made a mistake in my original post. There are lines before and after the interested parts but I'm only interested in the "keyword2 yyyy" part. Now I have 'keyword1(?s).*?(keyword2.*?)n', but this returns the whole string starts from keyword1: keyword1 xxxxx xxxx xxxx xxxxx keyword2 yyyy How do I just extract the "keyword2 yyyy"?
            – user2577547
            Nov 7 at 6:46










          • Hey, you can't move (?s) part in middle of regex as that is a flag in regex which tells dot can match new lines. I think you are capturing whole regex match. For capturing the interested text, you need to capture group 1's content.
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:00










          • If I don't move the (?s) in the middle it won't match anything because there 're multiple likes between keyword1 and keyword2. How do I get capture group 1's content?
            – user2577547
            Nov 7 at 7:34










          • (?s) is a global flag enabler. I think you are confusing it little bit with s. For capturing group 1's content your language must be providing some way. Like in Java, you write matcherObject.group(1). What language are you dealing with?
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:39










          • I'm using ANSI SQL. If I don't use (?s) it won't match the multiple lines, right?
            – user2577547
            Nov 7 at 21:35











          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',
          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%2f53184476%2fhow-to-use-regular-expression-to-match-strings-followed-by-some-keyword-and-mult%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          -1
          down vote



          accepted










          You can use this regex,



          ^(?s).*keyword1.*?(keyword2 yyyy).*$


          Explanation:




          • ^ --> start of string

          • (?s) --> Enables dot to match new lines

          • .* keyword1.*? --> Matches a string that contains keyword1 preceded and succeeded by any characters doing non-greedy match

          • (keyword2 yyyy) --> matches the string of your interest

          • .*$ --> followed by any characters and finally end of input


          Demo






          share|improve this answer





















          • Thank you Pushpesh! I made a mistake in my original post. There are lines before and after the interested parts but I'm only interested in the "keyword2 yyyy" part. Now I have 'keyword1(?s).*?(keyword2.*?)n', but this returns the whole string starts from keyword1: keyword1 xxxxx xxxx xxxx xxxxx keyword2 yyyy How do I just extract the "keyword2 yyyy"?
            – user2577547
            Nov 7 at 6:46










          • Hey, you can't move (?s) part in middle of regex as that is a flag in regex which tells dot can match new lines. I think you are capturing whole regex match. For capturing the interested text, you need to capture group 1's content.
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:00










          • If I don't move the (?s) in the middle it won't match anything because there 're multiple likes between keyword1 and keyword2. How do I get capture group 1's content?
            – user2577547
            Nov 7 at 7:34










          • (?s) is a global flag enabler. I think you are confusing it little bit with s. For capturing group 1's content your language must be providing some way. Like in Java, you write matcherObject.group(1). What language are you dealing with?
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:39










          • I'm using ANSI SQL. If I don't use (?s) it won't match the multiple lines, right?
            – user2577547
            Nov 7 at 21:35















          up vote
          -1
          down vote



          accepted










          You can use this regex,



          ^(?s).*keyword1.*?(keyword2 yyyy).*$


          Explanation:




          • ^ --> start of string

          • (?s) --> Enables dot to match new lines

          • .* keyword1.*? --> Matches a string that contains keyword1 preceded and succeeded by any characters doing non-greedy match

          • (keyword2 yyyy) --> matches the string of your interest

          • .*$ --> followed by any characters and finally end of input


          Demo






          share|improve this answer





















          • Thank you Pushpesh! I made a mistake in my original post. There are lines before and after the interested parts but I'm only interested in the "keyword2 yyyy" part. Now I have 'keyword1(?s).*?(keyword2.*?)n', but this returns the whole string starts from keyword1: keyword1 xxxxx xxxx xxxx xxxxx keyword2 yyyy How do I just extract the "keyword2 yyyy"?
            – user2577547
            Nov 7 at 6:46










          • Hey, you can't move (?s) part in middle of regex as that is a flag in regex which tells dot can match new lines. I think you are capturing whole regex match. For capturing the interested text, you need to capture group 1's content.
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:00










          • If I don't move the (?s) in the middle it won't match anything because there 're multiple likes between keyword1 and keyword2. How do I get capture group 1's content?
            – user2577547
            Nov 7 at 7:34










          • (?s) is a global flag enabler. I think you are confusing it little bit with s. For capturing group 1's content your language must be providing some way. Like in Java, you write matcherObject.group(1). What language are you dealing with?
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:39










          • I'm using ANSI SQL. If I don't use (?s) it won't match the multiple lines, right?
            – user2577547
            Nov 7 at 21:35













          up vote
          -1
          down vote



          accepted







          up vote
          -1
          down vote



          accepted






          You can use this regex,



          ^(?s).*keyword1.*?(keyword2 yyyy).*$


          Explanation:




          • ^ --> start of string

          • (?s) --> Enables dot to match new lines

          • .* keyword1.*? --> Matches a string that contains keyword1 preceded and succeeded by any characters doing non-greedy match

          • (keyword2 yyyy) --> matches the string of your interest

          • .*$ --> followed by any characters and finally end of input


          Demo






          share|improve this answer












          You can use this regex,



          ^(?s).*keyword1.*?(keyword2 yyyy).*$


          Explanation:




          • ^ --> start of string

          • (?s) --> Enables dot to match new lines

          • .* keyword1.*? --> Matches a string that contains keyword1 preceded and succeeded by any characters doing non-greedy match

          • (keyword2 yyyy) --> matches the string of your interest

          • .*$ --> followed by any characters and finally end of input


          Demo







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 6:25









          Pushpesh Kumar Rajwanshi

          2,0311716




          2,0311716












          • Thank you Pushpesh! I made a mistake in my original post. There are lines before and after the interested parts but I'm only interested in the "keyword2 yyyy" part. Now I have 'keyword1(?s).*?(keyword2.*?)n', but this returns the whole string starts from keyword1: keyword1 xxxxx xxxx xxxx xxxxx keyword2 yyyy How do I just extract the "keyword2 yyyy"?
            – user2577547
            Nov 7 at 6:46










          • Hey, you can't move (?s) part in middle of regex as that is a flag in regex which tells dot can match new lines. I think you are capturing whole regex match. For capturing the interested text, you need to capture group 1's content.
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:00










          • If I don't move the (?s) in the middle it won't match anything because there 're multiple likes between keyword1 and keyword2. How do I get capture group 1's content?
            – user2577547
            Nov 7 at 7:34










          • (?s) is a global flag enabler. I think you are confusing it little bit with s. For capturing group 1's content your language must be providing some way. Like in Java, you write matcherObject.group(1). What language are you dealing with?
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:39










          • I'm using ANSI SQL. If I don't use (?s) it won't match the multiple lines, right?
            – user2577547
            Nov 7 at 21:35


















          • Thank you Pushpesh! I made a mistake in my original post. There are lines before and after the interested parts but I'm only interested in the "keyword2 yyyy" part. Now I have 'keyword1(?s).*?(keyword2.*?)n', but this returns the whole string starts from keyword1: keyword1 xxxxx xxxx xxxx xxxxx keyword2 yyyy How do I just extract the "keyword2 yyyy"?
            – user2577547
            Nov 7 at 6:46










          • Hey, you can't move (?s) part in middle of regex as that is a flag in regex which tells dot can match new lines. I think you are capturing whole regex match. For capturing the interested text, you need to capture group 1's content.
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:00










          • If I don't move the (?s) in the middle it won't match anything because there 're multiple likes between keyword1 and keyword2. How do I get capture group 1's content?
            – user2577547
            Nov 7 at 7:34










          • (?s) is a global flag enabler. I think you are confusing it little bit with s. For capturing group 1's content your language must be providing some way. Like in Java, you write matcherObject.group(1). What language are you dealing with?
            – Pushpesh Kumar Rajwanshi
            Nov 7 at 7:39










          • I'm using ANSI SQL. If I don't use (?s) it won't match the multiple lines, right?
            – user2577547
            Nov 7 at 21:35
















          Thank you Pushpesh! I made a mistake in my original post. There are lines before and after the interested parts but I'm only interested in the "keyword2 yyyy" part. Now I have 'keyword1(?s).*?(keyword2.*?)n', but this returns the whole string starts from keyword1: keyword1 xxxxx xxxx xxxx xxxxx keyword2 yyyy How do I just extract the "keyword2 yyyy"?
          – user2577547
          Nov 7 at 6:46




          Thank you Pushpesh! I made a mistake in my original post. There are lines before and after the interested parts but I'm only interested in the "keyword2 yyyy" part. Now I have 'keyword1(?s).*?(keyword2.*?)n', but this returns the whole string starts from keyword1: keyword1 xxxxx xxxx xxxx xxxxx keyword2 yyyy How do I just extract the "keyword2 yyyy"?
          – user2577547
          Nov 7 at 6:46












          Hey, you can't move (?s) part in middle of regex as that is a flag in regex which tells dot can match new lines. I think you are capturing whole regex match. For capturing the interested text, you need to capture group 1's content.
          – Pushpesh Kumar Rajwanshi
          Nov 7 at 7:00




          Hey, you can't move (?s) part in middle of regex as that is a flag in regex which tells dot can match new lines. I think you are capturing whole regex match. For capturing the interested text, you need to capture group 1's content.
          – Pushpesh Kumar Rajwanshi
          Nov 7 at 7:00












          If I don't move the (?s) in the middle it won't match anything because there 're multiple likes between keyword1 and keyword2. How do I get capture group 1's content?
          – user2577547
          Nov 7 at 7:34




          If I don't move the (?s) in the middle it won't match anything because there 're multiple likes between keyword1 and keyword2. How do I get capture group 1's content?
          – user2577547
          Nov 7 at 7:34












          (?s) is a global flag enabler. I think you are confusing it little bit with s. For capturing group 1's content your language must be providing some way. Like in Java, you write matcherObject.group(1). What language are you dealing with?
          – Pushpesh Kumar Rajwanshi
          Nov 7 at 7:39




          (?s) is a global flag enabler. I think you are confusing it little bit with s. For capturing group 1's content your language must be providing some way. Like in Java, you write matcherObject.group(1). What language are you dealing with?
          – Pushpesh Kumar Rajwanshi
          Nov 7 at 7:39












          I'm using ANSI SQL. If I don't use (?s) it won't match the multiple lines, right?
          – user2577547
          Nov 7 at 21:35




          I'm using ANSI SQL. If I don't use (?s) it won't match the multiple lines, right?
          – user2577547
          Nov 7 at 21:35


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184476%2fhow-to-use-regular-expression-to-match-strings-followed-by-some-keyword-and-mult%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          横浜市

          Rostock

          Europa