检索特定模式内的值

发布于 2024-09-19 21:16:50 字数 432 浏览 3 评论 0原文

我有一个像这样的模式,

"The world is #bright# and #beautiful#"

我需要检索 # # 内的字符串“bright”,“beautiful”。任何指针


我的解决方案(感谢Bolu):

string s = "The world is #bright# and #beautiful#";
    string[] str = s.Split('#');
    for (int i = 0; i <= str.Length - 1; i++)
    {
        if (i % 2 != 0)
        {
            Response.Write(str[i] + "<br />");
        }
    }

i have a pattern as like this

"The world is #bright# and #beautiful#"

i need to retrieve the string "bright","beautiful" inside # # .. any pointers


My solution (thanks to Bolu):

string s = "The world is #bright# and #beautiful#";
    string[] str = s.Split('#');
    for (int i = 0; i <= str.Length - 1; i++)
    {
        if (i % 2 != 0)
        {
            Response.Write(str[i] + "<br />");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮邮递员 2024-09-26 21:16:50

如果你想要的只是 ## 中的字符串,那么不需要正则表达式,只需使用 string.Split:

string rawstring="The world is #bright# and beautiful";
string[] tem=rawstring.Split('#');

之后,你所需要的就是从 中获取偶数项(索引:1,3,5....) 字符串[]项

If all you want is the string inside ##, then no need for regex, just use string.Split:

string rawstring="The world is #bright# and beautiful";
string[] tem=rawstring.Split('#');

After that, all you need is to get the even item (with index: 1,3,5....) from the string[] tem

走野 2024-09-26 21:16:50

只要不能嵌套 #...# 序列,#([^#]+)# 就可以工作,并且会捕获 #' 之间的内容作为第一个反向引用

解释:

#        match a literal # character
(        open a capturing group
  [^     open a negated character class
     #   don't match # (since the character class is negated)
  ]+     close the class, match it one or more times
)        close the capturing group
#        match a literal # character

As long as you can't have nested #...# sequences, #([^#]+)# will work, and will capture the content between #'s as the first backreference.

Explanation:

#        match a literal # character
(        open a capturing group
  [^     open a negated character class
     #   don't match # (since the character class is negated)
  ]+     close the class, match it one or more times
)        close the capturing group
#        match a literal # character
初吻给了烟 2024-09-26 21:16:50

查看 Match 对象:

var match = Regex.Match(yourstring, @"The world is #(.*)# and beautiful")
var bright = match.Groups[1]

当然,当字符串中包含两个以上 # 时,这种情况就会失败。那么你可能想要进行非贪婪匹配。这可以使用正则表达式“#(.*?)#”来完成。这将匹配两个升号之间的最短字符串,并且仍然具有第一组中的内容。

Check out the Match object:

var match = Regex.Match(yourstring, @"The world is #(.*)# and beautiful")
var bright = match.Groups[1]

Of course this breaks down when you have more than two #'s in your string. Then you probably want to do a non-greedy match. This can be done with the regex "#(.*?)#". This will match the shortest string between two sharps and still have the contents in the first group.

濫情▎り 2024-09-26 21:16:50

您需要设置一个Capturing Group,方法是将要捕获的部分括在圆括号()中,并可选择指定捕获的名称:

Regex r = new Regex(@"#([^#]+?)#");

可以通过使用此代码:

Match m = r.Match("The world is #bright# and beautiful");
string capture = m.Groups[1];

或使用命名参数:

Regex r = new Regex(@"#(?<mycapture>[^#]+?)#");

可以使用此代码访问:

Match m = r.Match("The world is #bright# and beautiful");
string capture = m.Groups["mycapture"];

You need to set up a Capturing Group by wrapping the part you want to capture in round brackets () and optionally specifying a name for the capture:

Regex r = new Regex(@"#([^#]+?)#");

which can be accessed by using this code:

Match m = r.Match("The world is #bright# and beautiful");
string capture = m.Groups[1];

Or with a named parameter:

Regex r = new Regex(@"#(?<mycapture>[^#]+?)#");

which can be accessed by using this code:

Match m = r.Match("The world is #bright# and beautiful");
string capture = m.Groups["mycapture"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文