regex - Extracting text between two keywords or a keyword and \n -
i have set of lines of them follow format
startkeyword text want extract endkeyword\n
i want find these lines , extract information them.
note, text between keywords can contain wide range of characters (latin , non-latin letters, numbers, spaces, special characters) except \n
.
endkeyword
optional , can omitted.
my attempts revolving around regex
startkeyword (.+)(?:\n| endkeyword)
however capturing group (.+)
consumes many characters possible , takes endkeyword
not need.
is there way some text want extract
solely regular expressions?
you can make (.+) non greedy (which default greedy , eats whatever comes in way) adding ?
, add $
instead of \n
making more efficient
startkeyword (.+?)(?:$| endkeyword$)
if want \n
can use:
startkeyword (.+?)(?:\n| endkeyword\n)
see demo
Comments
Post a Comment