3

I need to split a string where I need to grab three pieces of information from that string, and putting it in array so essentially, the array will always have three thing: [first, second, third], and the second and third item can be blank.

The line will be in the form of "First Second, Id". I need to ignore the extra spaces after each word or before each word.

So the first and second word is distinguished by a space or spaces in between and the second word and Id is distinguished by a comma.

Examples of lines to split:

John Doe, 1234 => result: [John, Doe, 1234]

John [# spaces] Doe,[# spaces] 1234 => result: [John, Doe, 1234]

[# spaces] John [# spaces] Doe [# spaces] , [# spaces] 1234 => result: [John, Doe, 1234]

John , 1234 => result: [John,"",1234]

John => result: [John, "", ""]

I tried using the regex, line.split(/[\s,]+/), but it would only work for case 1.

How to create a regex that includes all these cases?

6
  • 1
    What does the regex return exactly when it fails? Instead of fixing the regex, there is always that dirty quick fix available, where you make the result become what you need after regex'ing. Commented Jun 17, 2015 at 15:36
  • it's only one space in your regex, use quantifiers, like /[\s,]+/ Commented Jun 17, 2015 at 15:38
  • How do you make sure that i always get three items in array whether it is empty or not. Commented Jun 17, 2015 at 15:47
  • hi guys, i just wanted to know, i solved it, thanks Commented Jun 17, 2015 at 15:55
  • @NewKidOnTheBlock: Then please post an answer Commented Jun 17, 2015 at 18:43

2 Answers 2

1

Tested on every case you provided...

Note: As per you examples there must be a comma after the second capture group to differentiate between two groups, or three.

All examples use .slice(1) to remove the first item from the returned array. This is because String.prototype.match returns an array including the original string.

Example-one: one.match(regex)["John Doe, 1234", "John", "Doe", "1234"]

Example-two: one.match(regex).slice(1)["John", "Doe", "1234"]

You can include the original string in the array if you want, but to answer your question with the most accuracy I could muster I sliced from index 1 to the end of the array.

var one = "John Doe, 1234";
var two = "John          Doe,       1234";
var three = "           John       Doe    ,      1234    ";
var four = "John , 1234";
var five = "John";
var six = ""; // additional test.
var seven = "John doe"; // additional test.
var eight = "John Doe,        " // additional test.

// Here is the regex...
var regex = /^\s*(\w*)\s*(\w*)\s*,?\s*(\w*)/;

one.match(regex).slice(1);
// result: ["John", "Doe", "1234"];

two.match(regex).slice(1);
// result: ["John", "Doe", "1234"];

three.match(regex).slice(1);
// result: ["John", "Doe", "1234"];

four.match(regex).slice(1);
// result: ["John", "", "1234"];

five.match(regex).slice(1);
// result: ["John", "", ""];

six.match(regex).slice(1);
// result: ["", "", ""];

seven.match(regex).slice(1);
// result: ["john", "doe", ""];

eight.match(regex).slice(1);
// result: ["John", "Doe", ""];

Also, when building a regex object using new RegExp, some of the characters must be escaped, which is why the double "\".

Sign up to request clarification or add additional context in comments.

1 Comment

To enable names like Mary-Jane, you can use [^\s,] (matches any character besides whitespace and commas) instead of \w.
0

My idea was to first remove extra spaces and commas, and then fire a regex that would search for the three components, looking specifically for two character groups, and one number group. I tried it in Python.

def get_name(namestr):
    returnable = []
    namestr = re.sub("(\s\s+)|(\,)", " ", namestr.strip())
    mat = re.match("([a-zA-Z]+)(\s+)?([a-zA-Z]+)?(\s+)?([0-9]+)?", namestr)
    if mat:
        return [mat.group(i) if mat.group(i) else '' for i in [1,3,5]]

You'll need to translate this to Javascript though. I tried but my poor command over the language took 20 minutes out of my life while just trying to remove the extra spaces.

Would be happy to see a suggested edit with JS implementation.

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.