正则表达式模式 - 匹配以 @ 开头的单词 [英] Regex pattern - match word that starts with @
问题描述
我的移动应用程序就像移动平台(WP7、Silverlight、.NET)上的论坛.一项功能是通过写入@"字符后跟用户名来标记其他用户.
在服务器端,PHP,我正在解析文本,使其与标签匹配并用更易读的字符串替换它们,例如 [tag](display name)|(user id)[/tag]
[tag](display name)|(user id)[/tag]代码>,但这对我们的目的来说并不重要.
为了匹配标签,我用空格替换了所有特殊字符,这样我就可以防止像 .... @name, ....
这样的情况.然后我将删除前一个命令可能已创建的所有多个空格.最后,我将每个空格分开,然后检查该单词是否以@"字符开头.这当然不是最好的方法,但这是我目前设法做到的.有一个弱点,换行符使我的代码失败.例如:
你好,这是我的第一行因为我去第二然后我标记@Jonh谁是好孩子
在这种情况下,我将在下面编写的代码失败.其中 $resp
是要解析的文本.
if (strpos($resp,'@') !== false) {$new_str = preg_replace('/[^a-zA-Z0-9_ \@]/', ' ', $resp);$new_str = preg_replace('/\s+/', ' ', $new_str);foreach(explode(' ', $new_str) as $word){如果 (strpos($word, '@') === 0) {//找到我的标签!}}}}
你建议怎么做?
与其使用正则表达式替换您不想匹配的所有内容,您应该能够立即匹配任何前面带有 @ 的单词.
$subject = "..blah.. @name,..blah..@hello,blah";$pattern = '/@\w+/';preg_match_all($pattern, $subject, $matches);print_r($matches);
输出:
Array ( [0] => Array ( [0] => @name [1] => @hello ) )
/@\w+/
假设只有数字、字母和下划线(这就是 \w
匹配的内容)是有效匹配(即@user123_xd),如果需要的话例如,在有效匹配(例如@user1a-12)中包含 -
(破折号),则 $pattern 将是 /@[\w-]+/
>
My mobile application is just like a forum on mobile platform (WP7, Silverlight, .NET). One feature consists in tagging other users by writing "@" char followed by the username.
On server side, PHP, I'm parsing the text so that it matches tags and replace them with more readable string such as [tag](display name)|(user id)[/tag]
, but that's not important for our purpose.
In order to match tags, I'm replacing all special chars with a space so I can prevent this like .... @name, ....
. Then I'm removing all multiple spaces that the previous command could have been created. And finally I'm splitting each whitespace and then I check if that word begins with "@" char.
This is not of course the best method, but It's what I managed to do so far. There's a weak point, new line chars make my code fail. For example:
Hello, this is my first line
since I go to second and then I tag
@Jonh
who is a good boy
In case like this, the code I'm going to write below fails.
Where $resp
is the text to parse.
if (strpos($resp,'@') !== false) {
$new_str = preg_replace('/[^a-zA-Z0-9_ \@]/', ' ', $resp);
$new_str = preg_replace('/\s+/', ' ', $new_str);
foreach(explode(' ', $new_str) as $word)
{
if (strpos($word, '@') === 0) {
//found my tag!
}
}
}
}
What would you advise to do?
Rather than using regex to replace everything you don't want to match, you should be able to immediately match any word with an @ before it.
$subject = "..blah.. @name, ..blah..@hello,blah";
$pattern = '/@\w+/';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
Output:
Array ( [0] => Array ( [0] => @name [1] => @hello ) )
/@\w+/
assumes that only numbers, letters and underscores (thats what the \w
matches) are valid matches (i.e. @user123_xd), if you want to include for example the -
(dash) in valid matches (e.g. @user1a-12) then the $pattern would be /@[\w-]+/
这篇关于正则表达式模式 - 匹配以 @ 开头的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!