Regex is grabbing preceding character

假装没事ソ 提交于 2019-12-11 04:05:47

问题


So I am experiencing some inconsistent behavior in my regex

My regex:

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

The input string:

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

When I run this in https://Regex101.com

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

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

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


回答1:


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\\\\)(.*)(?=\",)""")

"teststring" comes out

The string in your source comes out as

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

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

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

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\\\\)(.*)(?=\",)

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.



来源:https://stackoverflow.com/questions/43262175/regex-is-grabbing-preceding-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!