I am attempting to capture the word function
using Oniguruma regex (for VSCode syntax highlighting) into 2 different groups depending on if ::
are the preceding characters. As far as I know Oniguruma does not support conditionals, hence I thought I could capture the regex result into group 1 if the characters are not present and into group 2 if they are
Currently, I have the following:
(?>::\s*\b(function)\b)|\b(function)\b
Some text to test the code on:
integer function fun_with_func_space( function )
integer, intent(inout) :: function
end function fun_with_func_space
Intended regex match
Given the text above group $1
should
finds 1st and 2nd "function" in 'integer function fun_with_func_space( function )'
finds no match in 'integer, intent(inout) :: function'
finds "function" in 'end function fun_with_func_space'
group $2 on the other hand should
finds no match in 'integer function fun_with_func_space( function )'
find "function" in 'integer function fun_with_func_space( function )'
finds no match in 'end function fun_with_func_space'
As far as I understand the following should mean:
"Capture and exit into group 2 the word function
if preceded by :: else capture function
in group 1."
and according to regex101.com it should work: https://regex101.com/r/VtgeTD/1, yet my syntax highlighting is still failing. Am I doing something wrong?
CodePudding user response:
Try using a look behind:
(?:(?<=::|:: ))\b(function)\b|\b(function)\b
See live demo.
Or use a non-capturing group:
(?:::\s*)(function)\b|\b(function)\b
See live demo.