skip to Main Content

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


  1. modify your regular expression as:

    const regularEx = /([^s]+@[^s]+) - 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}/g;
    
    const myInput = '[email protected] - Fri, 19 May 2023 10:13:23 GMT Test Task from [email protected]';
    
    const match = regularEx.exec(myInput);
    const output = match[1]; // Extracting the captured group
    
    console.log(output); // [email protected] - Fri, 19 May 2023 10:13:23 GMT
    
    

    Explanation of the regular expression:

    • ([^s]+@[^s]+): This captures the email address by matching one or
      more 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 email
      address. 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.

    Login or Signup to reply.
  2. From what I understood, you want everything until the date… Dates all end in GMT so you can use this marker.

    /^.+?GMT/g
    

    This will simply take everything plus the first GMT met.

    Login or Signup to reply.
  3. This will do for your examples. I’ve just made a small change on your regex:

    "^[w.@]+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}"gm
    

    Just need to include all the characters that can be used in your user names/emails.

    Login or Signup to reply.
  4. Using a case insensitive pattern with the /i flag, you could use:

    ^.*?s-s[a-z]{3},sd{1,2}s[a-z]{3,9}sd{4}sd{1,2}:d{1,2}:d{1,2}s[a-z]{3}b
    

    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 month
    • d{4}sd{1,2}:d{1,2}:d{1,2} Match a year and time like part
    • s[a-z]{3}b Match 3 chars a-z followed by a word boundary to prevent a partial word match

    Regex demo

    const regex = /^.*?s-s[a-z]{3},sd{1,2}s[a-z]{3,9}sd{4}sd{1,2}:d{1,2}:d{1,2}s[a-z]{3}b/i;
    [
      '[email protected] - Fri, 19 May 2023 10:13:23 GMT Test Task from [email protected]',
      '[email protected] - Fri, 19 May 2023 10:13:23 GMT Test Task from [email protected]',
      'someName - Fri, 19 May 2023 10:13:23 GMT Test Task from someName'
    ].forEach(s => {
      const m = s.match(regex);
      if (m) {
        console.log(m[0]);
      }
    })

    A few alternative options

    If the part before - can not contain a hyphen, you might also use a negated character class:

    ^[^-]*s-s[a-z]{3},sd{1,2}s[a-z]{3,9}sd{4}sd{1,2}:d{1,2}:d{1,2}s[a-z]{3}b
    

    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:

    ^[^s@]+(?:@[^s@]+)?s-s[a-z]{3},sd{1,2}s[a-z]{3,9}sd{4}sd{1,2}:d{1,2}:d{1,2}s[a-z]{3}b
    

    Regex demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search