5

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.

BenH
  • 53

1 Answers1

2

but the problem is this regex only seems to work if there is an existence of that final "custom1=myvalue"

You can add an optional end of string to match instead of custom1=myvalue:
(?i)password(?-i)=.+?((?= (?i)custom(?-i)\d+=)|$)

Bonus points if you can strip out the "password=" to end up only with the actual password.

Use lookbehind when you match password=:
(?<=(?i)password(?-i)=).+?((?= (?i)custom(?-i)\d+=)|$)

  • Thanks Máté. I have been researching both options more since I posted and arrived at the exact solutions you have provided for both issues. I was coming back here to post those and ask if overall this regex was relatively optimal for what I was looking to do. The fact that you have suggested the same things I came up with is a good vote of confidence! – BenH Jun 09 '16 at 06:54
  • Actually, the solution I came up with was slightly different. I used "(?<=(?i)password(?-i)=).+?(?= (?i)custom(?-i)\d+=|$)" . In your answer you have used another set of parenthesis to group the lookahead. Both seem to work, can you explain why you need that extra grouping? thx. – BenH Jun 09 '16 at 06:59
  • I just wanted to make sure regex engine recognize options well, but you're right, that extra grouping isn't necessary – Máté Juhász Jun 09 '16 at 07:01