Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
@title是如何工作的,我如何在.Net的评论中找到提及_.net_Regex_Indexof_Mention - Fatal编程技术网

@title是如何工作的,我如何在.Net的评论中找到提及

@title是如何工作的,我如何在.Net的评论中找到提及,.net,regex,indexof,mention,.net,Regex,Indexof,Mention,我需要知道提及是如何工作的,如何在文本中找到提及的内容。 我们必须找到第一个'@'和最后一个而不是“^[a-zA-Z0-9,]+$” 感谢您分享您的经验 string comment=" hi @fri.tara3^"; mention is : "@fri.tara3" 看起来很适合正则表达式。有多种方法可以解决这个问题 这是最简单的一个: (?<mention>@[a-zA-Z0-9_.]+)[^a-zA-Z0-9_.] @[a-zA-Z0-9_.]+?(?![a-zA-Z

我需要知道提及是如何工作的,如何在文本中找到提及的内容。 我们必须找到第一个'@'和最后一个而不是“^[a-zA-Z0-9,]+$”

感谢您分享您的经验

string comment=" hi @fri.tara3^";
mention is : "@fri.tara3"

看起来很适合正则表达式。有多种方法可以解决这个问题

这是最简单的一个:

 (?<mention>@[a-zA-Z0-9_.]+)[^a-zA-Z0-9_.]
@[a-zA-Z0-9_.]+?(?![a-zA-Z0-9_.])
string comment = "hi @fri.tara3^ @hjh not a mention @someone";
const String pattern = "@[a-zA-Z0-9_.]+?(?![a-zA-Z0-9_.])";
var matches = Regex.Matches(comment, pattern);

for (int i = 0; i < matches.Count; i++)
{
    Console.WriteLine(matches[i].Value);
}
  • (?!)是消极的前瞻。意思是“只有在后面不跟这个时才匹配”
  • 命名捕获不是必需的,因为前瞻不使用前瞻部分
  • 它支持通过使用非贪婪量词
    +?
    添加多个提及查找。这确保了匹配提及尽可能短
“Lookaheads”是一个鲜为人知的概念,如果模式太长,它可能会成为阅读的一个难点。但这是一个有用的工具

使用C#的完整示例:

 (?<mention>@[a-zA-Z0-9_.]+)[^a-zA-Z0-9_.]
@[a-zA-Z0-9_.]+?(?![a-zA-Z0-9_.])
string comment = "hi @fri.tara3^ @hjh not a mention @someone";
const String pattern = "@[a-zA-Z0-9_.]+?(?![a-zA-Z0-9_.])";
var matches = Regex.Matches(comment, pattern);

for (int i = 0; i < matches.Count; i++)
{
    Console.WriteLine(matches[i].Value);
}
string comment=“hi@fri.tara3^@hjh不提某人”;
常量字符串模式=“@[a-zA-Z0-9_.]+?(?![a-zA-Z0-9_.])”;
var matches=Regex.matches(注释、模式);
for(int i=0;i
您已经知道可以使用正则表达式进行此操作。有什么问题吗?你在哪里卡住了?很难找到非“^[a-zA-Z0-9_,]+$”的最后一个索引,我不知道该怎么做do@ara:那怎么办?@WiktorStribiżew这太神奇了!难以置信,谢谢man@WiktorStribiżew-虽然原则是有效的,
\S
[a-zA-Z0-9,]
不一样,
\b
也与否定类不匹配。谢谢,找到multi@like
“hi@fri.tara3^@hjh”
我必须使用这样的东西吗:
comment=comment.Substring(indexofLastFind,…)
Regex可以在一次过程中为您找到所有匹配项。请参阅答案中的更新示例。您很神奇,令人难以置信!谢谢,非常感谢:*