4

I have a string with type, the expected results are

input = "[Peter Jane Minesotta <[email protected]>]"

output

Fname = "Peter"
SecondAndRemainingNames = "Jane Minesotta"
email = "[email protected]"

input = "[Peter  <[email protected]>]"

output

    Fname = "Peter"
    SecondAndRemainingNames = ""
    email = "[email protected]

I need to extract using regex

I have tried with

input.match(/\w/gim)

4 Answers 4

6

You can use

const rx = /\[(\S+)(?:\s+(.*?))?\s+<([^<>]+)>]/
const strings = ['[Peter Jane Minesotta <[email protected]>]','[Peter  <[email protected]>]'];
for (const s of strings) {
  const [_, Fname, SecondAndRemainingNames, email] = s.match(rx);
  console.log([Fname, SecondAndRemainingNames, email]);
}

See the regex demo.

Details

  • \[ - a [ char
  • (\S+) - Group 1: one or more non-whitespace chars (to stay within [...], you may use [^\s[\]]+ instead)
  • (?:\s+(.*?))? - an optional string of 1+ whitespaces followed with Group 2 capturing any zero or more chars other than line break chars as few as possible (replace .*? with [^[\]]*? if you want to stay within [...])
  • \s+ - one or more whitespaces
  • <([^<>]+)> - >, Group 3: one or more chars other than < and >, then >
  • ] - a ] char.
Sign up to request clarification or add additional context in comments.

Comments

2

You can use 3 different regex in order to simplify the problem. Also, you can rely on the structure of the string:

const input1 = "[Peter Jane Minesotta <[email protected]>]"

const input2 = "[Peter  <[email protected]>]"

function getFName(input) {
  const name =  input.match(/(?<=\[)\w+/);
  return name ? name[0] : '';
}

function getSNames(input) {
  const names =  input.match(/(?<!\[)(?<=\s)\w+(?=\s)/g);
  return names ? names.join(' ') : '';
}

function getEmail(input) {
  const mail =  input.match(/(?<=<)(?:\w|\.|@)+(?=>])/);
  return mail ? mail[0] : '';
}

const x = {
  name: getFName(input1),
  otherNames: getSNames(input1),
  mail: getEmail(input1)
};

console.log(x);


const y = {
  name: getFName(input2),
  otherNames: getSNames(input2),
  mail: getEmail(input2)
};

console.log(y);

Comments

1

This should give you what you want...

^\[(\w+)\s(?:((?:\w+\s?)*)\s)?<(.+)>\]$
  1. The first group (\w+) would capture the First Word (stops as soon as it finds space) which in your case would be the firstName

  2. The second group (?:((?:\w+\s?)*)\s)? would capture everything that between the last space (after firstName) and first occurrence of < which you want to save in SecondAndRemainingNames. Note: the ? at the end of this group makes occurrence of this pattern optional which is what you want as indicated by your 2nd example..

  3. Finally, the last group would capture everything that's between < and > which for you would be email

I've tested this pattern with both of your sample inputs and it's working as expected .. :)

Comments

0

This works fine:

var all = input.match(/(^\[\w+)|(\w+ )+|<.+>/gi);
var Fname = ""
var SecondAndRemainingNames = ""
var email = ""
if (all.length == 3) {
    Fname = all[0];
    SecondAndRemainingNames = all[1];
    email = all[2];
} else if (all.length == 2) {
    Fname = all[0];
    email = all[1];
}
Fname = Fname.substring(1);
if (SecondAndRemainingNames != "") {
    SecondAndRemainingNames = SecondAndRemainingNames.trim();
}
email = email.substring(1).slice(0, -1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.