Reputation: 37
edit: see Matching part of regex in substrings
I don't know if title is good but let me give some examples.
this is the pattern i came up with:
/(---+\s*([^\s-]+)\s*---+)(.*?)(---+\s*[^\s-]+\s*---+)/gs
These are scenarios:
--- content ---
lorem ipsum dolor sit amet
--- --- --- --- --- ---
Suspendisse nec dui in orci ullamcorper porttitor.
Sed lobortis dui ut placerat tempor. Donec lacus nibh, porta vitae mattis ac, facilisis dictum ipsum.
--- content ---
--- sub-content 1 ---
lorem ipsum dolor sit amet
--- --- --- --- --- ---
Suspendisse nec dui in orci ullamcorper porttitor.
Sed lobortis dui ut placerat tempor. Donec lacus nibh, porta vitae mattis ac, facilisis dictum ipsum.
--- sub-content 1 ---
--- sub-content 1 2 3 ---
lorem ipsum dolor sit amet
--- --- --- --- --- ---
Suspendisse nec dui in orci ullamcorper porttitor.
Sed lobortis dui ut placerat tempor. Donec lacus nibh, porta vitae mattis ac, facilisis dictum ipsum.
--- sub-content 1 2 3 ---
I can match if space count known but couldnt figured out where space count between non-space characters unknown. Basically i want to match all strings as long as there is a non-space character between first and end ---XXX---
Upvotes: 1
Views: 178
Reputation: 627410
You may use the following regex with a multiline flag (note you do not need any flag if you use the pattern in Ruby):
^(---+\s+([^\s-].*?)\s+---+)$(.*?)^(---+\s+([^\s-].*?)\s+--+)$
See the regex demo. I added line start/end anchors ^
and $
and changed [^\s-]+
that did not allow any intraword hyphens to [^\s-].*?
that requires a char other than whitespace and a hyphen and then anything up to the trailing hyphens.
Upvotes: 1