正则表达式查找包含一个数字的字符串,该数字具有一个空格,两个空格或根本没有空格,后跟/和四个数字 [英] RegEx find string containing numbers with one space, two spaces or no space at all followed by / and four numbers

查看:76
本文介绍了正则表达式查找包含一个数字的字符串,该数字具有一个空格,两个空格或根本没有空格,后跟/和四个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用RegEx在Word文档中查找字符串.该字符串包含数字{1; 5} ,一个空格,两个空格或根本没有空格,后跟/和四个数字{4},表示年份文档已创建.

I need to use RegEx to find a string in Word document. The string contains numbers{1;5} with one space, two spaces or no space at all followed by / and four numbers{4} which denotes a year in which the document was created.

我需要找到的字符串如下:

The strings I need to find are like these:

  • 45 /2017
  • 125 /2019
  • 1245 /2018
  • 12577 /2019
  • 37589 /2017
  • 45 /2017
  • 125 /2019
  • 1245 /2018
  • 12577 /2019
  • 37589 /2017

但有时两者之间没有空格:

but sometimes there is no space in between:

  • 45/2017
  • 125/2019
  • 1245/2018
  • 12577/2019
  • 37589/2017
  • 45/2017
  • 125/2019
  • 1245/2018
  • 12577/2019
  • 37589/2017

有时有两个空格:

  • 45 /2017
  • 125 /2019
  • 1245 /2018
  • 12577 /2019
  • 37589 /2017
  • 45 /2017
  • 125 /2019
  • 1245 /2018
  • 12577 /2019
  • 37589 /2017

,但始终位于同一位置-一个或两个空格.它总是位于第一个数字字符串之后,该数字不得超过5位-因此,我在RegEx中键入[0-9] {1; 5}.

but it's always in the same place - the space or two spaces. It's always after the first string of numbers, which are never more than 5 digits - therefore I type [0-9]{1;5} in RegEx.

但是,当我尝试在Word的Ctrl + H查找和替换对话框中构建此RegEx表达式,并针对Word的文档对其进行测试时,无法构建通用表达式来查找带有空格且没有空格的字符串.这 ?对我不起作用.我已经在这里阅读了?应该找到一个空格或没有空格,但是当我在Word文档中对其进行测试时,它会找到一个字符串,里面有两个空格,而没有找到一个空格或根本没有空格的字符串.

But when I tried to build this RegEx expression in Word's Ctrl+H find and replace dialog box, and I test it against the Word's document I cannot build universal expression to find these strings with space and no space. The ? is not working for me. I've read here that ? should find one space or no space, but when I'm testing it in my Word document it finds a string with two spaces inside, but not the one with one space or no space at all.

这是我尝试过的RegEx,无法同时匹配两个没有空格的字符串,如4125/2019以及带有一个或两个空格的类似字符串:

Here are the RegExs I've tried, which failed to match both strings with no space inside like 4125/2019, as well as similar strings with one or two spaces:

  1. [0-9]{1;5} ?/[0-9]{4}

[0-9]{1;5}\s?/[0-9]{4}

[0-9]{1;5}[ ]{0;2}/[0-9]{4}这给出了一个错误,但是[0-9]{1;5}[ ]{1;2}/[0-9]{4}找到125 /2019,但是没有找到125/201912577/2019.

[0-9]{1;5}[ ]{0;2}/[0-9]{4} this gives an error but [0-9]{1;5}[ ]{1;2}/[0-9]{4} finds 125 /2019 but doesn't find 125/2019 and 12577/2019.

在波兰语版本的MS Word/Excel中,我们在公式中使用了;而不是,,但是我可以全部使用冒号或分号来进行尝试.

In Polish version of MS Word/Excel we use ; instead of , in formulas, but I tried it all with colon or semi-colon interchangeably.

我已阅读以下来源:

Regex-空格或无空格

Analyst_Cave

正则表达式测试器

推荐答案

您可以使用单词通配符查找类似这样的表达式:

You can use a Word wildcard Find expression like:

<[0-9 ]{1;7}/[0-9]{4}>

无需涉及RegEx库.

without the need to involve the RegEx library.

这篇关于正则表达式查找包含一个数字的字符串,该数字具有一个空格,两个空格或根本没有空格,后跟/和四个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆