Escaping Asterisk Grabs wrong character

大兔子大兔子 提交于 2019-12-11 06:33:35

问题


When I run the code below I get the unexpected result where \* also captures É. Is there a way to make it only capture * like I wanted?

let s =
    "* A

ÉR

* B"
let result = System.Text.RegularExpressions.Regex.Replace(s, "\n(?!\*)", "", Text.RegularExpressions.RegexOptions.Multiline)
printfn "%s" result

Result After Running Replace

* AÉR
* B

Expected Result

"* A

ÉR
* B"

UPDATE

This seems to be working, when I use a pattern like so \n(?=\*). I guess I needed a positive lookahead.


回答1:


You're right that you need to use positive lookahead instead of negative lookahead to get the result you want. However, to clarify an issue that came up in the comments, in F# a string delimited by just "" is not quite like either a plain C# string delimited by "" or a C# string delimited by @"" - if you want the latter you should also use @"" in F#. The difference is that in a normal F# string, backslashes will be treated as escape sequences only when used in front of a valid character to escape (see the table towards the top of Strings (F#)). Otherwise, it is treated as a literal backslash character. So, since '*' is not a valid character to escape, you luckily see the behavior you expected (in C#, by contrast, this would be a syntax error because it's an unrecognized escape). I would recommend that you not rely on this and should use a verbatim @"" string instead.

In other words, in F# the following three strings are all equivalent:

let s1 = "\n\*"
let s2 = "\n\\*"
let s3 = @"
\*"

I think that the C# design is more sensible because it prevents confusion on what exactly is being escaped.



来源:https://stackoverflow.com/questions/43402043/escaping-asterisk-grabs-wrong-character

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