正则表达式:匹配包含至少3个数字或特定符号的8个字符单词

huangapple go评论64阅读模式
英文:

Regex: match 8 character words containing at least 3 instances of a digit or a specific symbol

问题

我试图创建一个正则表达式,匹配包含至少3个数字或'%'符号以及单词后面有空格的8个字符单词。

我的正则表达式目前是这样的:

const string = 'this word ab1d2f3h needs to go, but not this word abcdefgh %%%'.replace(/(?=(?:\D*(\d|%)){3})(\w|%){8}\s/g, '%%%%%%%% ');
英文:

I'm trying to create a regex that matches 8 character words that contain at 3 least instances of a digit or the '%' symbol and whitespace after the word.

The following strings should match:

  • 'ab1d2f3h '
  • 'ab%d2f3h '
  • 'ab%d%f3h '
  • 'ab%d%f%h '

The regex I have so far looks like this:

const string = 'this word ab1d2f3h needs to go, but not this word abcdefgh %%%'.replace(/(?=(?:\D*(\d|%)){3})(\w|%){8}\s/g, '%%%%%%%% ')

If I remove '%%%' from the string, it works - 'ab1d2f3h ' is replaced. However, if '%%%' is present in the string, it also replaces 'abcdefhg ', which I don't want to happen.

Does anyone know the proper regex for this?

答案1

得分: 1

如果您在使用模式的环境中支持回顾断言:(?<!\S)(?=\S{8}\s)(?:[^\s\d%]*[%\d]){3}\S*

解释

  • (?<!\S) 正向回顾,断言左侧有空白边界
  • (?=\S{8}\s) 正向前瞻,断言8个非空白字符后跟一个空白字符(注意它也可以匹配换行符)
  • (?:[^\s\d%]*[%\d]){3} 重复3次,匹配任何非空白字符(不是数字或%),然后匹配数字或%
  • \S* 匹配可选的非空白字符

Regex演示

或者使用捕获组:

(^|\s)(?=\S{8}\s)(?:[^\s\d%]*[%\d]){3}\S*

Regex演示

在替换中使用第一个捕获组以保留前导空白字符。

英文:

If a lookbehind assertion is supported in the environment where you are using the pattern:

 (?&lt;!\S)(?=\S{8}\s)(?:[^\s\d%]*[%\d]){3}\S*

Explanation

  • (?&lt;!\S) Positive lookbehind, assert a whitspace boundary to the left
  • (?=\S{8}\s) positive lookahead, assert 8 non whitespace chars followed by a whitespace char (note that it can also match a newline)
  • (?:[^\s\d%]*[%\d]){3} Repeat 3 times matching any non whitespace chars except a digit or % and then match either a digit or %
  • \S* Match optional non whitespace chars

Regex demo

Or using a capture group:

(^|\s)(?=\S{8}\s)(?:[^\s\d%]*[%\d]){3}\S*

Regex demo

In the replacement use the first capture group to keep the leading whitspace char.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const string = &#39;this word ab1d2f3h needs to go, but not this word abcdefgh %%%&#39;.replace(/(^|\s)(?=\S{8}\s)(?:[^\s\d%]*[%\d]){3}\S*/g, &#39;$1%%%%%%%%&#39;)
console.log(string)

<!-- end snippet -->

答案2

得分: 0

问了ChatGPT并得到了答案:

您可以使用以下正则表达式来匹配包含至少3个数字或符号“%”实例的8个字符单词:

\b(?=(\w*[\d%]){3})\w{8}\b

解释:

  • \b匹配词边界,确保正则表达式仅匹配整个单词。
  • (?=(\w*[\d%]){3})是一个正向先行断言,检查单词中至少有3个数字或符号“@”的存在。
  • \w{8}匹配任意8个单词字符(字母、数字和下划线)。
  • \b匹配单词边界的结尾。

请注意,此正则表达式假定字符串中没有空格或其他非单词字符。如果有,您可能需要修改正则表达式以处理它们。

英文:

Asked ChatGPT and it answered my question:

You can use the following regular expression to match 8 character words containing at least 3 instances of a digit or the symbol '%':

\b(?=(\w*[\d%]){3})\w{8}\b

Explanation:

  • \b matches a word boundary to ensure that the regex only matches whole words.
  • (?=(\w*[\d@]){3}) is a positive lookahead assertion that checks for the presence of at least 3 instances of a digit or the symbol '@' in the word.
  • \w{8} matches any 8 word characters (letters, digits, and underscores).
  • \b matches the end of the word boundary.

Note that this regex assumes that there are no spaces or other non-word characters in the string. If there are, you may need to modify the regex to handle them.

huangapple
  • 本文由 发表于 2023年4月1日 00:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定