donglu1973 2017-03-06 01:34 采纳率: 100%
浏览 95

PHP正则表达到C#.NET \ K [复制]

This question already has an answer here:

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

</div>
  • 写回答

1条回答 默认 最新

  • duangu6431 2017-03-09 03:05
    关注

    \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}");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)