Reputation: 326
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:
ATEST
BATEST
BATEST
BATEST
BATEST
Is it possible to optionally match a words between dot?
Thank you for the help.
Upvotes: 2
Views: 1350
Reputation: 626896
You can use
/(?:\w+\()+|\b(\w+(?:\.\w+)?)\b(?!')/g
If your JavaScript environment supports ECMAScript 2018+, you may use
/(?:\w+\()+|(?<!')\b(\w+(?:\.\w+)?)\b(?!')/g
See the regex demo #1 and regex demo #2. Details:
(?:\w+\()+
- match one or more sequences of one or more word chars and then (
char|
- or\b
- match a word boundary(?<!')
- '
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 .
and then one or more word chars\b
- a word boundary(?!')
- not followed with '
char.See the JavaScript demo:
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
:
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);
Upvotes: 2