I'm trying to match out a string which will have a format similar to the following:
username=joe password='this! is a password with "quotes" and spaces#913 custom1 customd afd weirdhuh? custom1=myvalue
To explain in more detail, this is a list of variables separated by equal signs. The valid variables that can be passed are: username, password, and customX (where X is any number of digit(s)).
I am specifically trying to match out the password field as the passed variable can technically have any number of quotes, spaces, etc that are valid characters. I have decided that the best "break point" to use to determine when the password string terminates is the existence of a "customX=" string which infers that the next variable is starting.
Therefore, in the above example the actual password would be:
'this! is a password with "quotes" and spaces#913 custom1 customd afd weird huh?
I have arrived at the following regex:
(?i)password(?-i)=.+?(?= (?i)custom(?-i)\d+=)
This appears to match the following:
password='this! is a password with "quotes" and spaces#913 custom1 customd afd weird huh?
This is in effect what I want (I can easily parse out the "password="), but the problem is this regex only seems to work if there is an existence of that final "custom1=myvalue" at the end of the string. If that is removed, then there is no match at all.
I need to be able to match the password string regardless if that final value is there or not.
Bonus points if you can strip out the "password=" to end up only with the actual password.
Best Answer
You can add an optional end of string to match instead of
custom1=myvalue
:(?i)password(?-i)=.+?((?= (?i)custom(?-i)\d+=)|$)
Use lookbehind when you match
password=
:(?<=(?i)password(?-i)=).+?((?= (?i)custom(?-i)\d+=)|$)