Search code examples
regexqtp

Regular Expression Words stuck together


Is there a way to write regular expressions to stop right before a particular word or characters?

For example, I have a text like:

Advisor:HarrisTeamTeamRole

So I want to write a regular expression that makes the advisor name dynamic, but only capture Harris. How do I write a regular expression to stop right before Team?


Solution

  • You could use a lookbehind and lookahead like this:

    (?<=Advisor:).*?(?=Team)
    

    Debuggex Demo

    This will only capture from "Advisor:" up to the first "Team", and the regex will not capture anything else after (including "Team") in a capture group or otherwise. This will require a type of regex that can do lookbehinds... if you are not using that, you'll have to use grouping... which could be as simple as:

    Advisor:(.*?)Team
    

    and then just get the capture group #1