I have inputs like –
input1 = '[email protected] - Fri, 19 May 2023 10:13:23 GMT Test Task from [email protected]'
input2 = '[email protected] - Fri, 19 May 2023 10:13:23 GMT Test Task from [email protected]'
input3 = 'someName - Fri, 19 May 2023 10:13:23 GMT Test Task from someName'
And expected outputs are –
output1 = [email protected] - Fri, 19 May 2023 10:13:23 GMT
output2 = [email protected] - Fri, 19 May 2023 10:13:23 GMT
output3 = someName - Fri, 19 May 2023 10:13:23 GMT
I want to extract output using regex.
I have tried with –
regularEx = /w+.[a-zA-Z]+s-sw{3},sd{1,2}s[a-zA-Z]{3,9}sd{4}sd{1,2}:d{1,2}:d{1,2}s[a-zA-Z]{3}/g
myInput = '[email protected] - Fri, 19 May 2023 10:13:23 GMT Test Task from [email protected]'
regularEx.exec(myInput);
This gives me the output –
[
"com - Fri, 19 May 2023 10:13:23 GMT"
]
But the expected output is –
[
"[email protected] - Fri, 19 May 2023 10:13:23 GMT"
]
How can I modify my regularEx to get desired output
4
Answers
modify your regular expression as:
Explanation of the regular expression:
([^s]+@[^s]+)
: This captures the email address by matching one ormore non-whitespace characters before and after the @ symbol.
w{3}, d{1,2} [A-Za-z]{3,9} d{4} d{1,2}:d{1,2}:d{1,2} [A-Za-z]{3}
: This matches the date and time pattern after the emailaddress. However, it is not captured as part of the desired output.
By modifying the regular expression as shown above, it will capture the email address and ignore the rest of the string.
From what I understood, you want everything until the date… Dates all end in GMT so you can use this marker.
This will simply take everything plus the first GMT met.
This will do for your examples. I’ve just made a small change on your regex:
Just need to include all the characters that can be used in your user names/emails.
Using a case insensitive pattern with the
/i
flag, you could use:Explanation
^
Start of string.*?s-s
Match as few as possible chars and then match a-
between whitespace chars[a-z]{3},sd{1,2}s[a-z]{3,9}s
The shortened day name, the day and the monthd{4}sd{1,2}:d{1,2}:d{1,2}
Match a year and time like parts[a-z]{3}b
Match 3 chars a-z followed by a word boundary to prevent a partial word matchRegex demo
A few alternative options
If the part before
-
can not contain a hyphen, you might also use a negated character class:If there can only be a single name without @ or an email like pattern, you can use an optional non capture group for the @ part:
Regex demo