0

Given a string of the form "(a,{b:c,d:e,f:g},h})", I want to extract a,c,e,g,h from the string. i.e. string will always contain 3 parameters, where 2nd parameter is of the form {b:c,d:e,f:g} i.e. it contains key value pairs and there can be any number of them. I want to extract all the values leaving behind keys.

Also I want to extract first and third parameter i.e. a and h in the above string. I am trying to scan the string and extract on character by character bases but I am not able to do extract values from 2nd argument.

Is there any efficient method to do it may be using regular expressions ?

2
  • why have that string? is it intentionally malformed? can you do something about the formatting? Commented May 26, 2012 at 8:02
  • @JosephtheDreamer yes it is intentionally stored as above format. Commented May 26, 2012 at 8:03

5 Answers 5

1

Try this regex:

\(([a-zA-Z0-9_\-]+),\{([a-zA-Z0-9_\-]+):([a-zA-Z0-9_\-]+),([a-zA-Z0-9_\-]+):([a-zA-Z0-9_\-]+),([a-zA-Z0-9_\-]+):([a-zA-Z0-9_\-]+)\},([a-zA-Z0-9_\-]+)\}\)

The first group is a, second is b, etc:

> str.match(/\(([a-zA-Z0-9_\-]+),\{([a-zA-Z0-9_\-]+):([a-zA-Z0-9_\-]+),([a-zA-Z0-9_\-]+):([a-zA-Z0-9_\-]+),([a-zA-Z0-9_\-]+):([a-zA-Z0-9_\-]+)\},([a-zA-Z0-9_\-]+)\}\)/)
["(a,{b:c,d:e,f:g},h})", "a", "b", "c", "d", "e", "f", "g", "h"]
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for not making the question much clearer. a,b,c,d,e,f,g,h all are some strings in themselves.
0

Here's a complicated solution using the versatile split function:

var str = "(a,{b:c,d:e,f:g},h)"
var outstr = "";

var parts = str.split(",");
for (var ixPart = 0; ixPart < parts.length; ++ixPart) {
    if (ixPart > 0) outstr += ",";
    var part = parts[ixPart];
    if (part.indexOf(":") > 0) {
        var parts2 = parts[ixPart].split(":");
        var part2 = parts2[1];
        if (part2.indexOf("}") >= 0)
           outstr += part2.substring(0, part2.indexOf("}"));
        else outstr += part2;
    } else {
       if (part.indexOf("(") == 0) outstr += part.substring(1);
       else if (part.indexOf(")") >= 0)
           outstr += part.substring(0, part.indexOf(")"));
       else outstr += part;
    }
}

return outstr;

2 Comments

BTW, I am assuming that the final } just before the final ) is a typo. That is, the } after the "h". If it isn't a typo, then my code needs further modification ...
I am pretty sure it will NOT work faster than regex processing. But it was kind of fun to write it.
0

How about:

var testString = "(a,{b:c,d:e,f:g},h)";

var parameterArray = testString.split(/\((.+?),\{.+?:(.+?),.+?:(.+?),.+?:(.+?)\},(.+?)\)/);

This assumes that the }) at the end of the sample string is a type-o, but it's easy to modify if not.

Comments

0

Divide and conquer!

function extract (str) {
  str = str.trim ().split (/\s*,\s*/); // split on , chars with optional surrounding spaces
  return str.map (function (v) { // create array from values
    // remove prefix and/or suffix from required values :
    // first ignore any leading ( or { chars
    // then ignore a single word followed by :
    // use the following trimmed string as the data
    // ignore any ) or } at the end  
    return (v.match (/^[({]*(?:\w\s*:\s*)?\s*(.*?)\s*[)}]*$/) || ['', v]) [1];
  });
}

This assumes that the data strings will never :

  1. be blank
  2. contain , characters
  3. begin with ( or {
  4. end with ) or }

Comments

0

If the format of the string is known as you described, I would first edit the string to make it into JSON and then use eval

Example:

var str1 = "('word1',{b:'word2',d:'word3',f:'word4'},'word5')";

// Edit the string to JSON format by replacing '(' and ')' with '[' and ']'
var str2 = str1.replace(/\(/, '[').replace(/\)/, ']')

// str2 is now => "['word1',{b:'word2',d:'word3',f:'word4'},'word5']"

var obj = eval(str2);

// All you results are now in obj.

var result1 = obj[0]; // This gets'word1'

//Similarly you can get the rest as follows:
obj[1].b // This gives you 'word2'
obj[1].d // 'word3'
obj[1].f // 'word4'
obj[2]   // 'word5'

If obj[1] has a variable number of key/value pairs and you want only value you can iterate over the object obj[1] as follows:

for (var key in obj[1]) {
  obj[1][key];  // This gives the values in each iteration
}

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.