Erel Segal-Halevi
Erel Segal-Halevi

Reputation: 36745

Challenging regular expression

There is a string in the following format:

I am looking for a regular expression that can separate the start from the rest.

For example, given the following string:

{{a}}{{b}} {{c}} def{{g}}hij

The two parts are:

{{a}}{{b}} {{c}}
def{{g}}hij

I tried this:

/^({{.*}})(.*)$/

But, it captured also the g in the middle:

{{a}}{{b}} {{c}} def{{g}}
hij

I tried this:

/^({{.*?}})(.*)$/

But, it captured only the first a:

{{a}}
{{b}} {{c}} def{{g}}hij

Upvotes: 0

Views: 92

Answers (5)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can use this:

(java)

string[] result = yourstr.split("\\s+(?!{)");

(php)

$result = preg_split('/\s+(?!{)/', '{{a}}{{b}} {{c}} def{{g}}hij');
print_r($result);

Upvotes: 1

HighBoots
HighBoots

Reputation: 293

I think I got it:

var string = "{{a}}{{b}} {{c}} def{{g}}hij";

console.log(string.match(/((\{\{\w+\}\})\s*)+/g));

// Output: [ '{{a}}{{b}} {{c}} ', '{{g}}' ]

Explanation:

  1. ( starts a group.
  2. ( another;
  3. \{\{\w+\}\} looks for {{A-Za-z_0-9}}
  4. ) closes second group.
  5. \s* Counts whitespace if it's there.
  6. )+ closes the first group and looks for oits one or more occurrences.

When it gets any not-{{something}} type data, it stops.

P.S. -> Complex RegEx takes CPU speed.

Upvotes: 1

Daniël Knippers
Daniël Knippers

Reputation: 3055

This keeps matching {{, any non { or } character 1 or more times, }}, possible whitespace zero or more times and stores it in the first group. Rest of the string will be in the 2nd group. If there are no parts surrounded by {{ and }} the first group will be empty. This was in JavaScript.

var str = "{{a}}{{b}} {{c}} def{{g}}hij";

str.match(/^\s*((?:\{\{[^{}]+\}\}\s*)*)(.*)/)

// [whole match, group 1, group 2]
// ["{{a}}{{b}} {{c}} def{{g}}hij", "{{a}}{{b}} {{c}} ", "def{{g}}hij"]

Upvotes: 1

Toto
Toto

Reputation: 91415

How about using preg_split:

$str = '{{a}}{{b}} {{c}} def{{g}}hij';
$list = preg_split('/(\s[^{].+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($list);

output:

Array
(
    [0] => {{a}}{{b}} {{c}}
    [1] =>  def{{g}}hij
)

Upvotes: 1

Oscar Bralo
Oscar Bralo

Reputation: 1907

I don´t know exactly why are you want to split, but in case that the string contains always a def inside, and you want to separate the string from there in two halves, then, you can try something like:

string text = "{{a}}{{b}} {{c}} def{{g}}hij";

            Regex r = new Regex("def");
            string[] split = new string[2];
            int index = r.Match(text).Index;
            split[0] = string.Join("", text.Take(index).Select(x => x.ToString()).ToArray<string>());
            split[1] = string.Join("", text.Skip(index).Take(text.Length - index).Select(x => x.ToString()).ToArray<string>());

// Output: [ '{{a}}{{b}} {{c}} ', 'def{{g}}hij' ]

Upvotes: 0

Related Questions