Chris
Chris

Reputation: 336

Regex match when not contains word

I have a question but I can't seem to figure it out. I have been referenceing this article: Regular expression to match a line that doesn't contain a word?

I am trying to match a URL if the URL doesn't contain 2 dashes:

Match = /test-doc.html

Non-Match = /test-doc--help.html

I have this which works to match and non-match: /(?<a>.(?!\-\-))*\.html

But the group "a" only gets 1 letter vs everything looking back. I would want group "a" to be "test-doc" rather than just the "c" at the end.

Any suggestions would be appreciated.

Upvotes: 1

Views: 1847

Answers (2)

You can try your expression in this website

http://gskinner.com/RegExr/

And also i think you should use this expression

"\/.*.html"

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149000

Try a pattern like this:

/(?<a>(?!.*--).*)\.html

This will match a literal / followed by zero more of any character, captured in group a, (but only if that sequence does not contain a literal --), followed by a literal .html.

For example:

Dim pattern As String = "/(?<a>(?!.*--).*).html"
Regex.Match("/test-doc-help.html", pattern).Groups("a").Value  // "test-doc-help"
Regex.Match("/test-doc--help.html", pattern).Groups("a").Value // ""

Upvotes: 2

Related Questions