简体   繁体   English

如何用数字表示 REGEX OR?

[英]How to express REGEX OR in in digits?

\d{1,2}|\d{5,}
(\d{1,2})|(\d{5,})

I want to match 1-2 digits or more than 5 digits.我想匹配 1-2 位或 5 位以上的数字。 Why both patterns above not working?为什么上面的两种模式都不起作用? I suppose this should match the second half:我想这应该与下半场相匹配:

s = 12345

S is a digit, not str. S是一个数字,而不是str。 But it doesn't match at all.但它根本不匹配。

You are missing word bounds.您缺少单词范围。 \b around the things you want to catch. \b 围绕你想要捕捉的东西。

Something like this should do the trick.像这样的东西应该可以解决问题。

\b\d{1,2}\b|\b\d{5,}\b

Here's a handy cheat sheet I like to use.这是我喜欢使用的方便的备忘单。 Link关联

  1. Remove the spaces around the pipe.删除 pipe 周围的空间。
  2. Put the largest regex in the first part of alternation: \d{5,}|\d{1,2}将最大的正则表达式放在交替的第一部分: \d{5,}|\d{1,2}
  3. Anchor your regex: ^(?:\d{5,}|\d{1,2})$锚定您的正则表达式: ^(?:\d{5,}|\d{1,2})$

Demo & explanation演示和解释

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

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