gpt4 book ai didi

c# - 无法理解为什么正则表达式不适用于字符串的开始/结束标记

转载 作者:行者123 更新时间:2023-11-30 21:47:14 26 4
gpt4 key购买 nike

这是我的带有选项 IgnoreCase 和 Singleline 的测试正则表达式:

^\s*((?<test1>[-]?\d{0,10}.\d{3})(?<test2>\d)?(?<test3>\d)?){1,}$

和输入数据:

      24426990.568   128364695.70706     -1288.460

如果我省略^(匹配行首)和$(匹配行尾)

\s*((?<test1>[-]?\d{0,10}.\d{3})(?<test2>\d)?(?<test3>\d)?){1,}

然后一切正常。

为什么它不适用于字符串开始/结束标记 (^/$)?

提前致谢。

最佳答案

The start and end is literally the start and end of the input string when in single line mode. It only means the start of the line and the end of the line in multiline mode.

请注意,这意味着整个输入字符串。

所以如果你使用:

      24426990.568   128364695.70706     -1288.460

作为您的输入字符串,那么开头是第一个空格,字符串的结尾将是 0

由于您的模式与您正在寻找的内容的一个实例完全匹配,因此当与 ^ 和 $ 一起使用时,正则表达式将失败。这是因为它在输入字符串中寻找该模式的一个实例,但实际上有三个。您有两个选择:

  1. 删除 ^ 和 $
  2. 更改模式以匹配至少一次

关于c# - 无法理解为什么正则表达式不适用于字符串的开始/结束标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38615598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com