Xavier Tobin
Xavier Tobin

Reputation: 43

How to fix regex not matching when a character is the first on a new line?

I have a regular expression I'm using to identify text that matches a markdown formatting rule, surrounded with asterisks.

I'm making sure the asterisks not only surround text, but that the asterisks are next to the text. e.g *bold* should work and * not bold * shouldn't, to avoid normal asterisks getting caught up in formatting.

This is my regex now: \*[^ ][^*]+.[^ ]\*

The problem is, this text is a positive match:

*Test
ing*

And this isn't:

*Test
g*

Both examples should match the expression.

I'm using regex101.com to test.

Any ideas?

Thanks in advance!

Upvotes: 1

Views: 77

Answers (3)

sschuberth
sschuberth

Reputation: 29811

Removing the dot as commented by @wiktor-stribiżew indeed fixes the issue, but the regex can be further simplified to \*\S[^*]+\S\*.

Note that this regex will also match the entire string of **Test**, i.e. it matches the double-asterisks. You didn't specify your intended behavior in this case.

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626794

The dot requires to match at least one character and the next negated character class matches any char but space obligatorily. Thus the dot fails as there is a newline before g.

Use

\*[^* ](?:[^*]*[^ *])?\*

Or

\*[^*\s](?:[^*]*[^\s*])?\*

These regexes match strings between asterisks containing at least one char.

Details

  • \* - an asterisk
  • [^*\s] - any one char other than asterisk and whitespace
  • (?:[^*]*[^\s*])? - an optional sequence of
    • [^*]* - zero or more chars other than asterisk
    • [^\s*] - any char other than asterisk and whitespace
  • \* - an asterisk.

Upvotes: 1

John C
John C

Reputation: 170

The following regexp matches both of your test cases:

\*[^ *]+.*?[^ ]\*

As an aside the site https://www.freeformatter.com/java-regex-tester.html (which I used to check your original regexp) is also an excellent place to develop and test regexp matchers and replacers.

Upvotes: 1

Related Questions