use this regex (?i)(?<=\bsubject:?)[^:].*$
Regex ? in lookbehind not consuming
Pergunta
Lookbehind with ? is not consuming the :
Regex:
(?i)(?<=\bsubject:?).+$
Text:
Subject: asdf adsf
Match
: asdf adsf
I don't want the : to be included in the match
If I search on:
Subject asdf adsf
It matches:
asdf adsf
That is the behavior I want
It appears to treat the : as optional but not consuming
If I match on just subject:? it is greedy and includes the :
Solução
Outras dicas
You have a question mark after the : which will make the colon optional.
(?i)(?<=\bsubject:).+$
This works ok for me:
(?i)(?<=subject:).+$
Remove the extra question mark.
Unless I've misunderstood, I don't think you need a look-ahead for this?
If you're just after whatever follows Subject
, then this is your regex:
Subject\:?(.+)$
And if you're after whatever follows Subject
, subject
or eg suBject
then this is your regex:
(?i)Subject\:?(.+)$
And include your word-boundary \b
again if that's still necessary:
\bSubject\:?(.+)$
or (?i)\bSubject\:?(.+)$
So, in C#.NET:
Regex r = new Regex(@"(?i)Subject\:?(.+)$");
Match m = r.Match("Subject asdf asdf");
Console.WriteLine(m.Groups[1]);
m = r.Match("Subject: asdf asdf");
Console.WriteLine(m.Groups[1]);
//Both output ' asdf asdf' - you might want to trim this.
//Or add optional space \s? after the optional colon \:? in your regex.
Or, rather than relying on magic constants to line up with implicitly numbered capture groups, explicitly name (and reference) the group:
string captureName = "yourcap";
Regex r = new Regex(@"(?i)Subject\:?(?<"+captureName+">.+)$");
Match m = r.Match("Subject asdf asdf");
Console.WriteLine(m.Groups[captureName]);