Accatyyc
Accatyyc

Reputation: 5828

RegexKitLite not matching square brackets

I'm trying to match usernames from a file. It's kind of like this:

username=asd123 password123

and so on.

I'm using the regular expression:

username=(.*) password

To get the username. But it doesn't match if the username would be say and[ers] or similar. It won't match the brackets. Any solution for this?

Upvotes: 2

Views: 298

Answers (2)

eldarerathis
eldarerathis

Reputation: 36203

I would probably use the regular expression:

username=([a-zA-Z0-9\[\]]+) password

Or something similar. Notes regarding this:

  • Escaping the brackets ensures you get a literal bracket.
  • The a-zA-Z0-9 spans match alphanumeric characters (as per your example, which was alphanumerc). So this would match any alphanumeric character or brackets.
  • The + modifier ensures that you match at least one character. The * (Kleene star) will allow zero repetitions, meaning you would accept an empty string as a valid username.
  • I don't know if RegexKitLite allows POSIX classes. If it does, you could use [:alnum:] in place of a-zA-Z0-9. The one I gave above should work if it doesn't, though.

Alternatively, I would disallow brackets in usernames. They're not really needed, IMO.

Upvotes: 1

Sadeq
Sadeq

Reputation: 8033

Your Regular Expression is correct. Instead, you may try this one:

username=([][[:alpha:]]*) password

[][[:alpha:]] means ] and [ and [:alpha:] are contained within the brackets.

Upvotes: 1

Related Questions