开发者

RegExp pattern to capture around two-characters delimiter

开发者 https://www.devze.com 2023-02-19 18:54 出处:网络
I have a string which is something like: prefix::key0==value0::key1==value1::key2==value2::key3==value3::key4==value4::

I have a string which is something like:

prefix::key0==value0::key1==value1::key2==value2::key3==value3::key4==value4::
开发者_StackOverflow中文版

I want to retrieve the value associated to a key (say, key1). The following pattern:

::key1==([^:]*)

...will work only if there are no ':' character in the value, so I want to make sure the pattern matching will stop only for the substring ::, but I'm can't find how to do that, as most examples I see are about single character matching.

How do I modify the regexp pattern to match all characters between "::key1==" and the next "::" ?

Thanks!


Can you do something like this : ::key1==(.*?)::? Assuming the language supports the lazy ? operator, this should work.


As mentioned in my comment to your question, if the entirety of your string is

prefix::key0==value0::key1==value1::key2==value2::key3==value3::key4==value4::

I would suggest exploding/splitting the string at :: instead of using regex as it will usually always be faster. You didn't specify language but here is a php example:

// string
$string = "prefix::key0==value0::key1==value1::key2==value2::key3==value3::key4==value4::";
// explode using :: as delimiter
$string = explode('::',$string);
// for each element...
foreach ($string as $value) {
  // check if it has == in it 
  if (strpos($value,'==')!==false) $matches[] = $value;
}
// output
echo "<pre>";print_r($matches);

output:

Array
(
    [0] => key0==value0
    [1] => key1==value1
    [2] => key2==value2
    [3] => key3==value3
    [4] => key4==value4
)

However, if you insist on the regex approach, here negative look-ahead alternative

::((?:(?!::).)+)

php example

// string
$string = "prefix::key0==value0::key1==value1::key2==value2::key3==value3::key4==value4::";
preg_match_all('~::((?:(?!::).)+)~',$string,$matches);
//output
echo "<pre>";print_r($matches);

output

Array
    (
        [0] => key0==value0
        [1] => key1==value1
        [2] => key2==value2
        [3] => key3==value3
        [4] => key4==value4
    )


I think you're looking for a positive look-ahead:

::key0==(.*?)(?=::\w+==)

With the following:

prefix::key0==val::ue0::key1==value1::key2==value2::key3==value3::key4==value4::

It correctly finds val::ue0. This also assumes the keys conform to \w ([0-9A-Za-z_])

Also, a positive look-ahead may be a bit of overkill, but will work if the answer contains ::, too.

0

精彩评论

暂无评论...
验证码 换一张
取 消