get text between an @ sign and a comma

时间:2022-02-22 02:20:13

So I am doing a list sort of like twitter where I want members to be able to tweat at each other.

所以我正在做一个像twitter的列表,我希望成员能够互相推文。

What I want to do is compose a regular expression that will extract all data between the @sign and a comma.

我想要做的是编写一个正则表达式,它将提取@sign和逗号之间的所有数据。

For instance,

 @foo, @bar, @foo bar, hello world

Currently I have the following expression.

目前我有以下表达方式。

 /@([a-z0-9_]+)/i

However, that will stop at the space so instead of registering "@foo bar" as on member it will recognize it at just @foo and ignore the bar portion.

但是,这将停留在空间,因此不会在成员上注册“@foo bar”,而是仅在@foo上识别它并忽略条形部分。

Can somebody help me alter that query so that usernames are allowed to have spaces.

有人可以帮我改变那个查询,以便允许用户名有空格。

5 个解决方案

#1


5  

~@([^,]+)~

[^,] means every char except ,

[^,]表示除了之外的每个字符,

DEMO

#2


1  

If you truly seek to match all the data between the @ sign and the comma, you can use

如果你真的想要匹配@符号和逗号之间的所有数据,你可以使用

/@(.+),/i

But what I think you need is...

但我认为你需要的是......

/@([\w +]+),/i

...which matches all word characters (letters, numbers and underscores!) and spaces between the @ and the comma signs. View the demo.

...匹配所有单词字符(字母,数字和下划线!)以及@和逗号之间的空格。查看演示。

#3


0  

I think this is what you need:

我想这就是你需要的:

/@([a-z0-9_\s]+)/i

\s is the symbol for space.

\ s是空间的象征。

#4


0  

/@(.*?),/ ? makes * non-greedy, thats why it's stop after first comma. You should understand that it won't match @foo without commas

/@(.*?),/?使*非贪婪,这就是为什么它在第一个逗号后停止。您应该明白,如果没有逗号,它将与@foo不匹配

#5


-2  

try preg_match_all

<?php
$subject = "@foo, @bar, @foo bar, hello world";
$pattern = '/@(.*),.*/';
$matches = preg_match($pattern, $subject, PREG_SET_ORDER);
print_r($matches);
?>

DEMO of REGEX PATTERN

REGEX PATTERN的演示

#1


5  

~@([^,]+)~

[^,] means every char except ,

[^,]表示除了之外的每个字符,

DEMO

#2


1  

If you truly seek to match all the data between the @ sign and the comma, you can use

如果你真的想要匹配@符号和逗号之间的所有数据,你可以使用

/@(.+),/i

But what I think you need is...

但我认为你需要的是......

/@([\w +]+),/i

...which matches all word characters (letters, numbers and underscores!) and spaces between the @ and the comma signs. View the demo.

...匹配所有单词字符(字母,数字和下划线!)以及@和逗号之间的空格。查看演示。

#3


0  

I think this is what you need:

我想这就是你需要的:

/@([a-z0-9_\s]+)/i

\s is the symbol for space.

\ s是空间的象征。

#4


0  

/@(.*?),/ ? makes * non-greedy, thats why it's stop after first comma. You should understand that it won't match @foo without commas

/@(.*?),/?使*非贪婪,这就是为什么它在第一个逗号后停止。您应该明白,如果没有逗号,它将与@foo不匹配

#5


-2  

try preg_match_all

<?php
$subject = "@foo, @bar, @foo bar, hello world";
$pattern = '/@(.*),.*/';
$matches = preg_match($pattern, $subject, PREG_SET_ORDER);
print_r($matches);
?>

DEMO of REGEX PATTERN

REGEX PATTERN的演示