genxgeek
genxgeek

Reputation: 13357

First regex instance match not matching on first line?

I have a regex pattern per the following example.

http://regex101.com/r/sB3kK5

Essentially it just matches the first instance of the specific regex pattern.

However, as a one off derivation I can get the following to match if there is just one pattern (matches first instance with blank line & a space above the pattern).

http://regex101.com/r/eY0iC7

However, if the pattern is on the very first line then there is no match.

http://regex101.com/r/qH7iT4

How can I get that last example to match on the first line?

Upvotes: 1

Views: 84

Answers (2)

zx81
zx81

Reputation: 41838

^(.+?)?\byo\b(?:(?!cut me:|yo).)*cut me:

I just added a ?after the (.+?) to make it optional.

demo

Upvotes: 1

Pshemo
Pshemo

Reputation: 124225

How can I get that last example to match on the first line?

Regex: ^(.+?)\byo\b(?:(?!cut me:|yo).)*cut me: requires one or more characters before by because of + in (.+?). Consider changing it to (.*?).

Upvotes: 3

Related Questions