3

I'm trying to replace all of the event handling functions whose name is either "eventHandler" or ends with "...EventHandler" to be encapsulated in a common event-handling function which does additional tasks before actually calling a event handler.

In short, I'm trying to do this (in sublime text editor) using the regular expression in find & replace:

loginEventHandler(args, callback) => processEventHandler(loginEventHandler, args, callback)

[ Find ]

(eventHandler|(?!processEventHandler)\w+EventHandler)((.*))

[ Replace ]

processEventHandler($1, $2)

This isn't working as expected. The find is also matching rocessEventHandler . How do I ignore matching the function if the function name is "processEventHandler"?

I tried solutions mentioned in the following questions, but didn't help.

Regular expression to match line that doesn't contain a word?

A regular expression to exclude a word/string

Here is the test result:

Regular Expression Test Result

sunilkumarba
  • 641
  • 2
  • 8
  • 17
  • You are using a regex tester that does not support Boost regex syntax. Use regex101 with PCRE as regex falvor. Also, please post the sample text in the question itself. – Wiktor Stribiżew Feb 01 '16 at 10:08
  • Here is the sample text: loginEventHandler(err, result) processEventHandler(loginEventHandler, err, result) eventHandler(err, result) appInitEventHandler(err, result) initProcessEventHandler(err, result) initprocessEventHandler(err, result) generic(e, r) – sunilkumarba Feb 01 '16 at 10:09
  • 1
    Try [`(?i)(\b(?!process)\w*EventHandler)\(([^)]*)\)`](https://regex101.com/r/dD9wI1/1) – Wiktor Stribiżew Feb 01 '16 at 10:13
  • How about this : https://regex101.com/r/dP7zD8/1 ? – Ankit Feb 01 '16 at 10:17
  • @AnkitMishra Can not use ^ as the function will not be at the beginning of the string. For example: return someEventHandler(e, r); – sunilkumarba Feb 01 '16 at 10:19

2 Answers2

2

Basically, your regex matches a part of the string with process is because the regex is not anchored at the start. If you need to match the strings at the start of a string, use ^. If you plan to just match them inside a larger text, use \b (word boundary).

You can use a regex with a negative lookahead anchored at the leading word boundary:

(?i)(\b(?!process)\w*EventHandler)\(([^)]*)\)

See regex demo

The regex (case-insensitive because of (?i) inline modifier) matches:

  • \b - a word boundary
  • (?!process) - a negative lookahead that fails a match if the following character sequence is found: \w* - zero or more alphanumeric symbols + EventHandler
  • \( - opening parenthesis
  • ([^)]*) - zero or more characters other than )
  • \) - closing parenthesis
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • The only thing I changed in your regular expression is, I used (.*) instead of ([^)]*) to match functions calls like the following one. `return loginEventHandler(new Error('Invalid credentials'), result);` – sunilkumarba Feb 01 '16 at 10:33
  • If these strings are always on one line, this is OK. If this is not the case, use `(?si)(\b(?!process)\w*EventHandler)\((.*?)\);` or even `(?si)(\b(?!process)\w*EventHandler)\((.*?)\);(?=\h*(?:$|\R))` – Wiktor Stribiżew Feb 01 '16 at 10:44
0

If the original EventHandler only has 2 arguments, this could work

[ Find ]

(eventHandler|\w+EventHandler)\((.*?),(.*)\)

[ Replace ]

processEventHandler($1, $2, $3)

ilomambo
  • 8,049
  • 10
  • 54
  • 101