0

I need to split a string to one or more substrings each of which contains no more or less than two dots. For example, if the string is foo.boo.coo.too" then what would be the regex to get the following array?: ["foo.boo.coo", "boo.coo.too"]. I hope there will be someone to answer this question - I will really admire you, as I've been programming for several years and have not still be used to regular expressions well enough to solve this particular problem by myself. Thank you very much in advance. Let me know your identity so that I can credit you as a contributor of the program I am creating.

Bob Torrent
  • 253
  • 1
  • 3
  • 7

2 Answers2

0

Regex by its nature will return non-intersecting results, so if you want "all matches" from a single regex - it's not possible.

So basically you will need to find first match, and then start from next position to find next match and so on; something like this technique described here regex matches with intersection in C# (it's not JavaScript but idea is the same)

You can use the following regex for example:

(?<=^|\.)((?:[^.]*\.){2}[^.]*?)(?=$|\.)

It ensures that it starts and ends with dot, or at begin/end of line, and contains exactly two dots inside, and captures result in first capture. You can replace * with + to make sure at least one symbol exists between dots, if it is required.

But you need to understand that such approach has really bad performance for the task you are solving, so may be using other way (like split + for) will be better solution.

Community
  • 1
  • 1
Lanorkin
  • 7,310
  • 2
  • 42
  • 60
  • Thank you! I understand the idea behind the non-intersecting. But should the regex example you mentioned work in JavaScript? Can you please create a jsfiddle/jsbin demo? – Bob Torrent Mar 21 '13 at 11:54
  • And as for the bad performance, why is it really that bad for solving my task? – Bob Torrent Mar 21 '13 at 12:01
0

RegEx is for this Problem not the best solution a similar problem was discussed here: split-a-sting-every-3-characters-from-back-javascript

A good javascript solution would be a javascript function like this

function splitter(text){
  var parts = text.split(".");
  var times = parts.length - 2;
  var values = [];
  for(var index = 0; index<times;index++)
  {
    values.push(parts.slice(index,index+3).join("."));
  }
  return values;
}

splitter("too.boo.coo.too")
//=> Result tested on Chrome 25+ ["too.boo.coo", "boo.coo.too"]

I hope this helps

If you want to Use Regex try the Lookhead Stuff, this could help http://www.regular-expressions.info/lookaround.html

Community
  • 1
  • 1
winner_joiner
  • 12,173
  • 4
  • 36
  • 61