正则表达式 - 精确提取6位数

Yoa*_*oav 0 c# regex

Yoav - 编辑更清晰

嗨,我需要在文本文件中找到6位数字字符串.我在C#工作.例:

text text text123365 text text text
Run Code Online (Sandbox Code Playgroud)

表达式必须跳过超过6的字符串:

text text text1233656 text text text
Run Code Online (Sandbox Code Playgroud)

上面的字符串不应该返回任何结果,因为数字字符串的长度是7.

我想出了这个表达式: [^0-9]([0-9]){6}[^0-9]

它完美地工作,除了在行的开头或结尾的字符串

123365text text text text text text
text text text text text text123365
Run Code Online (Sandbox Code Playgroud)

是否可以识别这些案例?

Pat*_*and 6

尝试:

(?<!\d)\d{6}(?!\d)
Run Code Online (Sandbox Code Playgroud)

它说:

  • 找到6位数字,这些数字不是直接在数字之前或之后

它将在字符串中的任何位置查找.

例子:

123365文本文本文本文本文本文本文本文本文本文本text123365

火柴:

  1. 123365
  2. 123365

123365文本文本文本234098文本文本文本文本文本文本文本567890 text123365

火柴:

  1. 123365
  2. 234098
  3. 567890
  4. 123365


Blu*_*kMN 6

System.Text.RegularExpressions.Regex re = 
    new System.Text.RegularExpressions.Regex(@"(^|\D)(\d{6})($|\D)");
Run Code Online (Sandbox Code Playgroud)


Ian*_*hes 5

我认为你最好使用负向前瞻和后视来做这个而不是边界或不匹配,比如:

(?<![0-9])[0-9]{6}(?![0-9])
Run Code Online (Sandbox Code Playgroud)