简体   繁体   English

正则表达式:如何匹配两个精确指定的4位数字之一

[英]Regexp: How to match one of two exactly specified 4 digit numbers

I am looking at a large file in vim that has a whole lot of numbers in it. 我正在看vim中的一个大文件,里面有很多数字。 I am looking for a specific record that has the number 1119 in one field, and I know 2 records later there is a number 1196 in the same field. 我正在寻找一个在一个字段中具有数字1119的特定记录,而我知道2条记录以后在同一字段中具有数字1196。

I would like to search for either but have both highlighted, 我想搜索其中一个,但都突出显示了,

I have tried: 我努力了:

[1119|1196] which matches any of these single digits like 1.
{1119|1196} doesn't find anything (I am not sure what it looks for).
{1119,1196}

doesn't find anything either. 也没有找到任何东西。 I am surprised at this, as I think it should look for either 1119 or 1196, and those numbers definitly exist lots of times in the file. 我对此感到惊讶,因为我认为它应该查找1119或1196,并且这些数字在文件中确实存在很多次。

I have also tried putting /g for global at the end, but that did not help either. 我也尝试将/ g放在末尾,但这也无济于事。

I also tried 我也试过

(1119|1196), and that did not work either.

I also tried 我也试过

((1119)|(1196)) and that did not work either.

Any ideas? 有任何想法吗?

See VimRegex.com : 参见VimRegex.com

4.6 Alternations 4.6变更
Using \\| 使用\\| you can combine several expressions into one which matches any of its components. 您可以将多个表达式组合成一个与其任何组成部分匹配的表达式。 The first one matched will be used. 将使用第一个匹配的内容。

\\(Date:\\|Subject:\\|From:\\)\\(\\s.*\\)

So, you need to escape special characters: 因此,您需要转义特殊字符:

\(1119\|1196\)

Since the grouping here is applied to the whole pattern that can be referenced with \\0 ( \\0 -> the whole matched pattern ), you can safely use 由于此处的分组适用于可以用\\0引用的整个模式( \\ 0->整个匹配的模式 ),因此您可以放心使用

1119\|1196

to search for the two numbers. 搜索两个数字。

However, if you need to find the two numbers as whole words, use \\< / \\> word boundaries (then, grouping is fine): 但是,如果您需要找到两个数字作为一个完整的单词,请使用\\< / \\>单词边界(然后可以分组):

\<\(1119\|1196\)\>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM