简体   繁体   English

正则表达式匹配字符串中的单词或单词之间的点(可选)

[英]Regex to match a word or a dot between words (optionally) in a string

I'm currently working on an issue that matches a word between special characters.我目前正在处理一个与特殊字符之间的单词匹配的问题。 Optionally matches words in between dot.可以选择匹配点之间的单词。

I currently tried below regex pattern it works fairly well in words that have dot in between but I struggled to make it work with word without dot.我目前尝试使用正则表达式模式,它在中间有点的单词中效果很好,但我很难让它与没有点的单词一起使用。

[^\W\s]+\.+[^\s\W)]+

Given the string:给定字符串:

  ,DECODE(T3.ATEST,' ',T3.BATEST
                           ,DECODE(NVL(T4.BATEST,'9') 
                                 ,'1',NVL(T4.BATEST,' ')
                                     ,T3.BATEST))      

it matches the following:它匹配以下内容:

T3.ATEST
T3.BATEST
T4.BATEST
T4.BATEST
T3.BATEST

Now trying this regex pattern above without dot in between:现在尝试上面没有点的正则表达式模式:

  ,DECODE(ATEST,' ',BATEST
                           ,DECODE(NVL(BATEST,'9') 
                                 ,'1',NVL(BATEST,' ')
                                     ,BATEST))      

I need a regex that also has the output below:我需要一个下面还有 output 的正则表达式:

ATEST
BATEST
BATEST
BATEST
BATEST

Is it possible to optionally match a words between dot?是否可以选择匹配点之间的单词?

Thank you for the help.感谢您的帮助。

You can use您可以使用

/(?:\w+\()+|\b(\w+(?:\.\w+)?)\b(?!')/g

If your JavaScript environment supports ECMAScript 2018+, you may use如果您的 JavaScript 环境支持 ECMAScript 2018+,您可以使用

/(?:\w+\()+|(?<!')\b(\w+(?:\.\w+)?)\b(?!')/g

See the regex demo #1 and regex demo #2 .请参阅正则表达式演示 #1正则表达式演示 #2 Details :详情

  • (?:\w+\()+ - match one or more sequences of one or more word chars and then ( char (?:\w+\()+ - 匹配一个或多个单词字符的一个或多个序列,然后( char
  • | - or - 或者
  • \b - match a word boundary \b - 匹配单词边界
  • (?<!') - ' is not allowed to be immediately to the left of the current location (?<!') - '不允许紧跟在当前位置的左边
  • (\w+(?:\.\w+)?) - Group 1: one or more word chars and then an optional occurrence of a . (\w+(?:\.\w+)?) - 第 1 组:一个或多个单词字符,然后可选地出现一个. and then one or more word chars然后是一个或多个单词字符
  • \b - a word boundary \b - 单词边界
  • (?!') - not followed with ' char. (?!') - 不跟'字符。

See the JavaScript demo:请参阅 JavaScript 演示:

 const text = ",DECODE(T3.ATEST,' ',T3.BATEST\n,DECODE(NVL(T4.BATEST,'9')\n,'1',NVL(T4.BATEST,' '),T3.BATEST)),DECODE(ATEST,' ',BATEST,DECODE(NVL(BATEST,'9'),'1',NVL(BATEST,' '),BATEST))"; const regex = /(?:\w+\()+|\b(\w+(?:\.\w+)?)\b(?;')/g, let results=[];m. while(m = regex.exec(text)) { if (m[1];== undefined) { results.push(m[1]); } } console.log(results);

If your JavaScript environment supports matchAll :如果您的 JavaScript 环境支持matchAll

 const text = ",DECODE(T3.ATEST,' ',T3.BATEST\n,DECODE(NVL(T4.BATEST,'9')\n,'1',NVL(T4.BATEST,' '),T3.BATEST)),DECODE(ATEST,' ',BATEST,DECODE(NVL(BATEST,'9'),'1',NVL(BATEST,' '),BATEST))"; const regex = /(?:\w+\()+|\b(\w+(?:\.\w+)?)\b(?;')/g. const results = Array.from(text,matchAll(regex). x => x[1]);filter(x =>.;x); console.log(results);

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

相关问题 正则表达式:匹配两个字符串之间的特定单词 - Regex: Match specific words between two string 正则表达式匹配两个词之间的字符串,其中结束边界词是可选的 - Regex to match string between two words, where the ending boundary word is optional 大括号正则表达式之间的匹配点 - Match dot between braces regex 正则表达式匹配字符串中的字母和数字,可选地具有一组特殊字符 - Regex to match letters and digits in a string optionally having a set of special characters JS Regex:在单引号之间匹配字符串中的单词 - JS Regex: Match word in a string between single quotes 正则表达式匹配字符串中的2个不同的单词 - regex to match 2 different words in a string 如何匹配两个单词之间的字符串,并为字符串中所有两个定义的单词重复此模式,Regex? - How to match string between two words, and repeat this pattern for all two defined words in the string, Regex? 使用正则表达式匹配一个或多个单词的一部分 - Using regex to match part of a word or words 正则表达式匹配一个单词后跟一个字符串,并允许多个单词(最多5个单词)介于两者之间 - Regex matching a word not followed by a string and allow multiple words (upto 5 words) in between RegEx将匹配字符串中最后一次出现的点 - RegEx that will match the last occurrence of dot in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM