Reputation: 21
I try to match a simple pattern P1 (whitespaces or tabs, then ## then text):
[ \t]*##\S*
But sometimes, I want to match with another pattern P2 (neither whitespaces nor tabs, then ## then text):
[^ \t]*##\S*
P2 must be used when ##\S* follow \S*
P1 must be used otherwise
Examples of expected match results:
##foo
must give
##foo
##foo (6 whitespaces before pattern)
must give
##foo (because there is some whitespaces before the pattern ##foo and not any non-whitespace characters)
foo ##bar
must give
##bar (because there is some non-whitespace charecters before the pattern ##foobar)
foo bar ##foobar
must give
##foobar
I tried lookbehind method but it's not possible because there is no fixed size.
It would be very nice if someone could help me...
Upvotes: 2
Views: 75
Reputation: 20656
You can achieve the effect of what you're after using capture groups:
^(\s*##\S*)|^\S+\s*(##\S*)
Then something like
## foo
will be matched by the first alternative, and the result can be obtained from the first capture group, whereas
foo ## bar
will be matched by the second alternative, and the "## bar" can be obtained from the second capture group.
Upvotes: 1