需要定期EX pression,提取子字符串在一些指定的格式 [英] Need regular expression that extracts sub-strings that are in some specified format

查看:150
本文介绍了需要定期EX pression,提取子字符串在一些指定的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函​​数,提取所有开始与一些字符子串:

 公共静态列表<串GT; GetStringsStartingWith(字符串文字,字符字符)
    {
        清单<串GT;输出=新的List<串GT;();
        的foreach(在Regex.Matches匹配匹配(文字,@(小于?!\\ w)的+人物+ @\\ W +))
        {
            尝试
            {
                output.Add(match.Value.Replace(character.ToString(),));
            }
            赶上(的NullReferenceException){继续; }
        }
        返回输出;
    }

它的正常工作与普通字符串如 @Test 。但现在我有一些字符串具有以下格式:


  

您好,这是一个@ [TEST1]。[TEST2]。[TEST3]字符串。


现在,我要的是一个普通的前pression,可以提取 @ [TEST1]。[TEST2]。[TEST3] 从上面的字符串。和可以有许多这样的串的格式。例如,某些字符串可能只是 @ [测试] @ [TEST1]。[TEST2]

以上的功能是无法提取字符串并返回

更新

我也想提取的子串有之间的空格[] 。像这样的:


  

@ [1:联系人:丹尼尔Zahariev] + @ [2:​​请联系:Dankajuro]。[1:联系人:丹尼尔
  Zahariev]


其实这就像我们使用Facebook或计算器评论的用户标签。我的系统检查字符串时,有一个 @ 它会检查,如果它是一个正常@(电子邮件等)或再presenting的标签。这是通过检查下一个字符后 @ 检查,如果它是 [然后之间的信息[ ] 是用户信息。其他的一切只是一个普通的文本。

因此​​,对于上面的字符串输出应该是:

  @ [1:联系人:丹尼尔Zahariev]
@ [2:​​请联系:Dankajuro]


解决方案

您可以试试这个正则表达式:

  Regex.Matches(文字,@(小于?!\\ W)| [?* \\] +人物+ @(\\ w + \\ B \\(= [\\ S +] | $)))

要赛后 @

所有的非空格字符

正则表达式演示

I have following function that extracts all the sub-strings starting with some character:

public static List<string> GetStringsStartingWith(string text, char character)
    {
        List<string> output = new List<string>();
        foreach (Match match in Regex.Matches(text, @"(?<!\w)" + character + @"\w+"))
        {
            try
            {
                output.Add(match.Value.Replace(character.ToString(), ""));
            }
            catch (NullReferenceException) { continue; }
        }
        return output;
    }

It's working fine for normal strings like @test. But now I have some strings that have following format:

Hello, this is a @[test1].[test2].[test3] string.

Now, what I want is a regular expression that can extract @[test1].[test2].[test3] from above string. And there can be a number of formats of such strings. For example, some string might be just @[test] or @[test1].[test2].

Above function is unable to extract the string and is returning null.

UPDATE

I also want to extract those sub-strings that have spaces between [ ]. Like this one:

@[1:contact:Daniel Zahariev]+@[2:contact:Dankajuro].[1:contact:Daniel Zahariev]

Actually these are like user tags that we use on facebook or stackoverflow comments. My system checks the string and when there is a @ it checks if it is a normal @(for email and others) or representing a tag. This is checked by checking next character after @ if it is [ then the information between [ and ] is user info. Everything else is just a normal text.

So for above string output should be:

@[1:contact:Daniel Zahariev]
@[2:contact:Dankajuro]

解决方案

You can try this regex:

Regex.Matches(text, @"(?<!\w)" + character + @"(\w+\b|\[.*?\](?=[\s+]|$))")

To match all non-space characters after @

RegEx Demo

这篇关于需要定期EX pression,提取子字符串在一些指定的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆