Regex: Match word containing special characters

6.6k views Asked by At

I am currently using a oniguruma regex to search for function like matches with the exception of some keywords. Ex, in the string "this is a test() and im() testing() thi[s]() this_[is]_a_fun(with,some,params)"

the regex should match: test, im, testing, thi[s] this_[is]_a_fun The current regex I'm using is

\s*([A-z0-9\w_]+).?(?=\()(\b(?<!if|while|for|return|else|elif|equals|or|xor|and|not|le|gre)) 

but this does not match thi[s] or any function containing brackets in the word.

I tried to update the regex to match these patterns with the regex

\s*([A-z0-9\w_|\[|\]]+).?(?=\()(\b(?<!if|while|for|return|else|elif|equals|or|xor|and|not|le|gre)) 

but to no avail.

Any help to match these kind of patterns would be greatly appreciated

1

There are 1 answers

5
Ankit On BEST ANSWER

Try this RegEx:

([\w+\[\]]+)(?=\([\w+,]*\))

Demo
Details:

  • \w+ matches any word character (equal to [a-zA-Z0-9_]) one or more times
  • \[ matches the character [ literally
  • \] matches the character ] literally
  • + matches 1 or more times
  • Positive Lookahead (?=\([\w+,]*\)) matches the characters ( and any characters from [\w+,] and the character ) literally.
  • [\w+,]* matches any word character (equal to [a-zA-Z0-9_]) and , zero or more times.