正则表达式正在抢先角色 [英] Regex is grabbing preceding character

查看:59
本文介绍了正则表达式正在抢先角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在正则表达式中遇到了一些不一致的行为

So I am experiencing some inconsistent behavior in my regex

我的正则表达式:

(?<=test\\\\)(.*)(?=\",)

输入字符串:

"test.exe /c echo teststring > \\\\.\\test\\teststring",

当我在 https://Regex101.com

我得到值teststring,但是当我在F#中运行它时

I get the value teststring however when I run this in F#

Regex.Match(inputString, "(?<=test\\\\)(.*)(?=\",)")

我拿回\teststring.我的目标是只获取teststring.我不确定自己在做什么错.

I get \teststring back. My goal is to get just teststring. I'm not sure what I'm doing wrong.

推荐答案

我成功使用了三重引号引起来的字符串.然后只需要考虑正则表达式转义符,而不考虑F#字符串转义符.

I had success using triple quoted strings. Then only the regex escapes need be considered, and not the F# string escapes.

let inputString = """test.exe /c echo teststring > \\\\.\\test\\teststring","""
let x = Regex.Match(inputString, """(?<=test\\\\)(.*)(?=\",)""")

测试字符串"出现

源代码中的字符串显示为

The string in your source comes out as

(?<=test\\)(.*)(?=",)

如果您不想使用三引号或逐字记录,则必须使用F#编写:

If you don't want to use triple quotes or verbatim, you will have to write this in F# :

"(?<=test\\\\\\\\)(.*)(?=\\\",)"

F#中的此字符串使用反斜杠来转义反斜杠和引号字符.一行中有八个反斜杠,然后在字符串值中变成四个实际的反斜杠.还有这个:

This string in F# uses backslashes to escape backslashes and a quote character. There are eight backslashes in a row in one place, and this then becomes four actual backslashes in the string value. There is also this:

\\\"

转换为实际字符串值中的一个实际\和一个实际.

which translates to one actual \ and one actual " in the actual string value.

所以我们最后得到的字符串值为

So then we end up with a string value of

(?<=test\\\\)(.*)(?=\",)

这是输入到正则表达式引擎的实际字符串值.像F#编译器一样,正则表达式引擎也使用反斜杠来转义字符.因此,任何实际的反斜杠都必须加倍,然后再加倍.

This then is the actual string value fed to the regex engine. The regex engine, like the F# compiler, also uses the backslash to escape characters. That's why any actual backslash had to be doubled and then doubled again.

这篇关于正则表达式正在抢先角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆