Search code examples
regexansibleregex-greedy

regex parsing issue ansible


I am trying to grab the password via regex . But having difficulty grabbing the entire password:

Password: Wi!CYtB!%w8z    (Please change it immediately!)

This is the regex I wrote to grab it and it works:

[^Password: ].+[^    (Please change it immediately!)]

The problem starts if the password has (a or t or s or u or i at the end) not able to grab the entire password only grabbing upto z

Password: Wi!CYtB!%w8za (Please change it immediately!)

Not sure how to fix this issue


Solution

  • If the language you're using supports Positive Lookbehind, Try this regex

    (?<=Password: )[^\s]*
    

    Demo: https://regex101.com/r/8xxwhz/1/

    • (?<=Password: ) Positive Lookbehind is used here (?<=) to match whatever comes after this string Password:.
    • [^\s] to match anything that is not a space.

    If your language doesn't support Positive Lookbehind, you could use groups. Example:

    Password: ([^\s]*)
    

    Make sure that you will use group (1) for the password.

    Demo: https://regex101.com/r/8xxwhz/2/