0

I am having trouble converting this Regex pattern:

"\bGOTO\s+\K\S+"

into a Regex that can be used with C#.NET. The problem seems to be that \K cannot be used, but not being well versed in Regular Expressions, I don't know how I would go about fixing the problem. Any help would be appreciated.

An explicit conversion from \K to c# would probably help alot with using it in other Regexes, too.

EDIT: The Perl reference table to c# didn't help me that much, as I could not find an explicit conversion from \K to something compatible with c#. I looked at what \G was, but that does not appear to be what I am looking for

micahneitz
  • 59
  • 1
  • 7
  • 3
    Why oh why don't you just put a capture group around it `\bGOTO\s+(\S+)"` ? If you think the engine is doing less work using the `\K` construct, think again, it's not. If it's an issue of getting just what you want in group 0, then use this `(?<=\bGOTO\s+)\S+"` –  Mar 06 '17 at 02:05
  • Thank you, sln, the second one worked perfectly. – micahneitz Mar 14 '17 at 16:57

1 Answers1

0

\K Sets the given position in the regex as the new "start" of the match. This means that nothing preceding the K will be captured in the overall match.

However, if you want to insert it into a string in C#, you need to add escaped character so the string would be recognized as a Regex pattern or use verbatim string. Also, in the .net framework, you need to add a group name for the \K<GroupName> Here are snippets you could find useful:

Regular Regex:

\bGOTO\s+\k<TextHere>\S

Escaped Character:

\\bGOTO\\s+\\k<TextHere>\\S

Implementation in C#

Regex rgx = new Regex("\\bGOTO\\s+\\k<TextHere>\\S");
        foreach (Match m in rgx.Matches("String to match"))
            Console.WriteLine($"{m.Value} at -> {m.Index}");
Dany Gagnon
  • 90
  • 1
  • 8
  • It did not seem to work for me. I found the answer I needed in the comments now. Thank you for trying to help me, maybe your answer will help someone else. – micahneitz Mar 14 '17 at 16:59