开发者

string placeholder and regular expressions

I have created a method where I can search for string placeholders, this I do with Regular expressions. At the moment I try to expand this method by adding grouping features.

For example if I have this string:

"Hallo {g:test1} asdasd {p:test1} sdfsdf{o:test1}"

I want to :

  1. Search for the string test1, even if there is standing a letter:(like g:) before it.
  2. I want to search for: all strings with for example a g: before it.

I can't really figure out how to do this in C# can someone help me?

At the moment I programmed this:

private string test() {
      string pattern = @"\{(.*?)\}";
      string query = "Hallo {g:test1} asdasd {p:test1} sdfsdf{o:test1}";

      v开发者_高级运维ar matches = Regex.Matches(query, pattern);

      foreach (Match m in matches) {
        Test = m.Groups[1].Value;
      }

      return Test;
    }


Try this:

 \{(?:.:)?(.*?)\}

It will match the text not including the letter and the colon which may be before it.

To limit this to strings with a particular letter before it:

 \{(?:#:)(.*?)\}   replacing # with the letter you are filtering on

e.g.

 \{(?:g:)(.*?)\} 


  1. \{.:test1\}
  2. \{g:.+?\}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜