[英]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.