regexlookbehind

Another way of getting Positive look behind


is there any alternative to the positive lookbehind? I ask because I created the following regex that I will use in a specific software:

(?<=function )[a-zA-Z]([A-Za-z]|[0-9])*

However, apparently this software does not accept this form as a valid regex. So I would need a regex that uniquely takes the word followed by "function". Could anyone let me know how this could be done or if this could actually be done? The only way to do this that I know of is the lookbehind.


Solution

  • Are you trying to find the string "function" followed by a function name with letters and digits? No lookarounds are necessary.

    function [a-zA-Z][A-Za-z0-9]*
    

    Note that your original [A-Za-z]|[0-9] is better written as [A-Za-z0-9].

    EDIT: If you want just the word after "function" then you use a capture group:

    function ([a-zA-Z][A-Za-z0-9]*)
    

    How you use the capture group depends on the tool or language you're using, which you didn't specify.