Home > Net >  How to get the text between two strings
How to get the text between two strings

Time:01-02

I found lots of answers in Stack Overflow, but none of them worked for me.

I tried this one:

(?<=\/\*start\*\/)(.*)(?=\/\*end\*\/)

and this one:

\/\*start\*\/(\t*|\s*|\n*|.*)\/\*end\*\/

and my text is:

/*start*/
something
/*end*/

How can I get from /*start*/ to /*end*/? (start and end included)

CodePudding user response:

I am assuming you have a new lines within the searched string, and not sure if it is \r\n or \r or \n (depends on from where you are getting the text). I think, you can modify it as deemed to your usage from there on.

So, how about:

/(\/\*start\*\/)(\r\n|\r|\n)(.*)(\r\n|\r|\n)(\/\*end\*\/)/

CodePudding user response:

This will do the trick

  /\*start\*/[\s\S].*?/\*end\*/

CodePudding user response:

If you don't want to cross matching /*start*/ and /*end*/ in between, you can use:

/\*start\*/(?:\r?\n(?!/\*(?:start|end)\*/).*)*\r?\n/\*end\*/

The pattern matches:

  • /\*start\*/ Match /*start*/
  • (?: Non capture group
    • \r?\n Match a newline
    • (?!/\*(?:start|end)\*/) Assert not either /*start*/ and /*end*/ at the start of the line
    • .* Match the whole line
  • )* Close the non capture group and optionally repeat it to get all lines
  • \r?\n/\*end\*/ Match a newline and /end/`

Note that if the delimiter of the pattern is not a forward slash, you don't have to escape the \/

REgex demo

CodePudding user response:

With awk you can do:

gawk '/\/\*start\*\//,/\/\*end\*\//'  inputfile

This will print all lines between two regular expressions.

Normally you use gawk '/start/,/end/' but in above start is replaced by /*start*/, and the * and / are escaped using a \.

CodePudding user response:

I did it by this:

\/\*start\*\/(.|\r?\n)*\/\*end\*\/
  • Related