2

So i have a file that contains emails like this :

[email protected]:theirUsername

[email protected]:theirUsername

...

i want to extract emails that start with PA and then follow up with 6 random characters, then @gmail.com and after that :theirUsername which is not important for me what the username is.

i tried using this :

egrep "^PA[a-zA-Z0-9-.+_]{6}@gmail.com:[a-zA-Z0-9-.+_]+"

i tried this with grep and still didn't work But its not working? its been a long time since i have used regex with egrep and reading the manual didn't help either, what am i doing wrong here? what is the correct way to type it?

1 Answer 1

2

If you really just want "6 random characters" and you don't care about what is after the : then you can simplify easily. Don't try to match what's after the : at all, and just use a . for the characters after PA

egrep '^PA.{6}@gmail.com:'

e.g.

% cat f
hello
[email protected]:jgkfd
[email protected]
[email protected]:

% egrep '^PA.{6}@gmail.com:' f
[email protected]:jgkfd
4
  • Thanks it worked! but can you explain why my regex didn't work? when i use that regex it says invalid range end, what am i doing wrong with that regex? Commented Nov 3, 2018 at 16:23
  • The - should be the last character in the [...] pattern if you want it to be a literal -. So [a-zA-Z0-9-.+_] should be [a-zA-Z0-9.+_-] instead Commented Nov 3, 2018 at 16:55
  • Damn i had no idea order matters inside [ ], so it should always be in that order? Commented Nov 3, 2018 at 18:15
  • If you put a - in the middle then it thinks it's a range, such as 0-9 or A-Z. By putting it at the end, regexp knows you don't mean a range. Commented Nov 4, 2018 at 1:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .