Rachael
Rachael

Reputation: 1995

Regex exclude hyphen as right endpoint of capture (VB.NET)

I have the following string (in vb.net):

Dim data as String = "-- Compiled: Aug  6 2015 10:07:08 --\nanotherline--\n..."

I'd like the output of regex.match() to look like the following:

Aug  6 2015 10:07:08

I'm trying variations of this:

Regex.Match(data, "(?<=Compiled:\s)(.*)(?<!-)", RegexOptions.IgnoreCase).ToString

but the best I can output is:

Aug  6 2015 10:07:08 --\nanotherline--\n...

How do I get regex to acknowledge the hyphen ("dash") character to stop matching?

Thank you in advance!

Upvotes: 1

Views: 75

Answers (3)

anubhava
anubhava

Reputation: 785721

You don't to need to use lookbehind at RHS, just use negation pattern:

Regex.Match(data, "(?<=Compiled:\s)([^-]*)\s-)", RegexOptions.IgnoreCase).ToString

RegEx Demo

Upvotes: 1

vks
vks

Reputation: 67988

(?<=Compiled:\s)(.*?)(?=-{2})

Use a lookahead instead.See demo.

https://regex101.com/r/eX9gK2/9

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174806

You need to use positive lookahead with a non-greedy regex.

Regex.Match(data, "(?<=Compiled:\s).*?(?=\s*-)", RegexOptions.IgnoreCase).ToString

DEMO

Upvotes: 2

Related Questions