我正在寻找一个可以用正则表达式完成的解决方案.我知道这将变得容易变量,子串等.
我正在寻找PCRE风格的正则表达式语法,即使我提到vim.
我需要识别4个数字的字符串,它们不能全为0.所以以下字符串将是一个匹配:
- 0001
- 1000
- 1234
- 0101
这不会:
- 0000
这是一个子字符串,将发生在大字符串中的设置位置,如果这很重要;我不认为应该.例如
- xxxxxxxxxxxx0001xxxxx
- xxxxxxxxxxxx1000xxxxx
- xxxxxxxxxxxx1234xxxxx
- xxxxxxxxxxxx0101xxxxx
- xxxxxxxxxxxx0101xxxxx
- xxxxxxxxxxxx0000xxxxx
- (?<!\d)(?!0000)\d{4}(?!\d)
或者更善意/可维护/正确地:
- m{
- (?<! \d ) # current point cannot follow a digit
- (?! 0000 ) # current point must not precede "0000"
- \d{4} # match four digits at this point,provided...
- (?! \d ) # that they are not then followed by another digit
- }x