来自字符串:
public static void main(String args[])
我需要提取3个字符串的数组:
{
我目前所做的事情:我只需class MyModel < ActiveRecord::Base
serialize :content
attr_accessible :content, :title
end
代币"@user do foo, and bar please, #urgent. contact xyz"
,["@user", "do foo, and bar please, #urgent", "contact xyz"]
,indexOf()
并获取其间的子字符串。
我想用RegEx优化它。我怎样才能找到几个标记并将它们组合在一起?就像在数组中一样?
答案 0 :(得分:0)
你可以这样做:
List<string> output = new List<string>();
string input = "@user do foo, and bar please, #urgent. contact xyz";
string pattern = @"(@[\w]+)"; // inclusive split, @User will be included in the split result
string[] regexSplit = Regex.Split(input, pattern);
foreach (string str in regexSplit)
{
// exclusive split, . won't be included in split result
string[] res = str.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
output.AddRange(res);
}
请注意: \ w表示任何字母数字字符 (@ [\ w] +):表示@符号后跟一个或多个字母数字字符,我们将它们放在括号中,因此它将成为包容性分割。