[英]Why does this regex [^\/]\d{4} not match a 4 digit number at the begining of a line?
The pattern [^\\/]\\d{4}
works for 2014(i)
and 01/01/2015
but not 2014(i)
when it is the first item in the text file. 模式
[^\\/]\\d{4}
适用于2014(i)
和01/01/2015
但不适用于2014(i)
因为它是文本文件中的第一项。
You are looking for a character before digits and at the beginning of line / string there is no character right before them. 您要在数字前寻找一个字符 ,并且在行/字符串的开始处没有字符。 As @JonSkeet mentioned in comments since no character before digits is found at beginning of a line,
[^\\/]
consumes first digit so number of left digits doesn't match \\d{4}
. 正如@JonSkeet在评论中提到的那样,因为在行首找不到数字前的字符,所以
[^\\/]
消耗了第一位数字,因此剩余数字位数与\\d{4}
不匹配。 That said, you are looking for this: 也就是说,您正在寻找:
(?<!\/)\d{4}
(?<!\\/)
a negative lookbehind which assures no /
before digits (an assertion, no character consuming applies) (?<!\\/)
在后面的否定表示 ,确保数字前没有/
(断言,不消耗字符) or if you have no problem with matching preceding character: 或匹配前面的字符没有问题:
(?:^|[^\/])\d{4}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.