简体   繁体   English

使用正则表达式的子串字边界

[英]Substring word boundaries with regex

I'm 99% of there with a regex to find words, but not any iterations of where that occurs as a substring.我有 99% 的人使用正则表达式来查找单词,但没有将其作为子字符串出现的任何迭代。

ie Looking for jam or JAM but not Pa jam a or floro jam即寻找jamJAM但不是 Pa jam a 或 floro jam

This is what I have so far :这是我到目前为止

\bjam\s?\d?\b

It works with all the combinations I'm likely to encounter except that I would like it to also pick up on multiple iterations of the same string with no spaces它适用于我可能遇到的所有组合,除了我希望它也能在没有空格的情况下接收同一字符串的多次迭代

jamjamjamjam

Is there away I can add that to the regex?我可以将它添加到正则表达式吗?

You may use this regex with alternation:您可以交替使用此正则表达式:

~\b(?:(?:jam)+|jam\h*\d*)\b~i

RegEx Demo正则表达式演示

RegEx Details:正则表达式详情:

  • \\b : Word boundary \\b : 字边界
  • (?:(?:jam)+|jam\\h*\\d*) : Match 1+ adjacent jam strings or jam followed by 0+ whitespaces and 0+ digits (?:(?:jam)+|jam\\h*\\d*) :匹配 1+ 个相邻的jam字符串或jam后跟 0+ 个空格和 0+ 个数字
  • \\b : Word boundary \\b : 字边界
  • i : mode for ignore case i : 忽略大小写模式

You could try something like this :你可以尝试这样的事情:

\b(?:jam|JAM)+\b

https://regex101.com/r/C05fsI/4 https://regex101.com/r/C05fsI/4

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

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