问题
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