Reputation: 5828
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
Reputation: 36203
I would probably use the regular expression:
username=([a-zA-Z0-9\[\]]+) password
Or something similar. Notes regarding this:
a-zA-Z0-9
spans match alphanumeric characters (as per your example, which was alphanumerc). So this would match any alphanumeric character or brackets.+
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.[: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
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