1

我需要从字符串数组中提取一个 Skype 帐户。它们出现在字符串中的单词“skype:”之后。所以我需要使用正则表达式提取它们的方法。看起来我需要类似的东西: string RegexPattern = @"skype:\s*(\w*)"; 但是,当此正则表达式与 . (点)像 P.Revenko.Sokolniki 它只返回第一个字母(在这种情况下为 P),并且它必须是 P.Revenko.Sokolniki。任何帮助将不胜感激。

4

3 回答 3

4

编辑:使用这个:

skype:\s*(?<account>[\w.]*)

并像这样获取名为帐户的组:

String skypeAccount = Regex.Match(inputString, @"skype:\s*(?<account>(\w|\.)*)").Groups["account"].Value;
于 2012-07-04T07:10:31.890 回答
2

如果它们都由分号分隔,则不需要正则表达式。

但是,使用 Regex 的解决方案如下:

string pattern = "(?<=skype:\s*)(?<account>[^\s]+)";

解释

(?<=skype:\s*)    --look behind for an instance of "skype:" followed by any number of spaces
(?<account>       --named capture group called "account
    [^\s]+        --match any character that is not a space, and do it at least once.
)                 --group closure

但是,正如我之前所说,RegEx 确实不需要,您可以简单地使用少量字符串操作。

string rawSkype = "skype: example1 ; skype: example2.com";
string[] skypeNames = Array.ConvertAll(rawSkype.Split(';'), 
                                       raw => raw.Replace("skype:", "").Trim());

这也很容易。

于 2012-07-04T07:21:24.377 回答
0

你需要把\w\。放入方括号中

string c = "skype: P.Jon.Doe";

Regex ex = new Regex(@"^skype:\s*[\w\.]*");

if (ex.IsMatch(c))
{
    Console.WriteLine(ex.Match(c).Value);
}
于 2012-07-04T07:20:01.747 回答