简体   繁体   中英

regex how to extract strings between delimiters and not double delimiters

I'm sure this is a duplicate but cannot find the right search criteria.

Basically I have a user supplied string with keywords enclosed in braces, I want a regex that will find the keywords but will ignore double sets of the delimiters.

example: "A cat in a {{hat}} doesn't bite {back}."

I need regex that will return {back} but not {hat}.

This is for C#.

这就是你要找的东西

(?<!\{)\{\w+\}(?!\})

Description

Try this out, this will require the open and close brackets to be single. Double brackets will be ignored.

See also this permlink example

[^{][{]([^}]*)[}][^}]

在此输入图像描述

c# example

using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "A cat in a {{hat}} doesn't bite {back}.";
          Regex re = new Regex(@"[^{][{]([^}]*)[}][^}]");
          MatchCollection mc = re.Matches(sourcestring);
          int mIdx=0;
          foreach (Match m in mc)
           {
            for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
              {
                Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
              }
            mIdx++;
          }
        }
    }
}

$matches Array:
(
    [0] => Array
        (
            [0] =>  {back}.
        )

    [1] => Array
        (
            [0] => back
        )

)

The answer will vary slightly depending on which regex parser you are using, but something like the following is probably what you want:

(?:[^{]){([^}]*)}|{([^}]*)}(?:[^}])|^{([^}]*)}$

Non "{" (not part of the match) followed by "{" (capturing) all the non "}" chars (end capturing) followed by "}", or...

"{" followed by "{" (capturing) all the non "}" chars (end capturing) followed by non "}" (not part of the match), or...

Start-of-line followed by "{" (capturing) all the non "}" chars (end capturing) followed by end-of-line

Note that some parsers may not recognize the "?:" operator and some parsers may require that some or all of the following chars (when not inside of "[]") be backslash escaped: { } ( ) |

使用正则表达式助手不太难:

(?:[^{]){([^}]*)}|{([^}]*)}(?:[^}])|^{([^}]*)}$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM