Regex Find and Replace:删除除以“开始”的单词以外的所有内容#&引用;?
我是regex的新手,找不到一个简单的方法。我想删除所有不以#开头的单词,并在它们之间加一个逗号,例如,如果我有:Regex Find and Replace:删除除以“开始”的单词以外的所有内容#&引用;?,regex,keyboard-maestro,Regex,Keyboard Maestro,我是regex的新手,找不到一个简单的方法。我想删除所有不以#开头的单词,并在它们之间加一个逗号,例如,如果我有: Cookie Recipe for n00bs #cookie #recipe #chocolate To do this you have to etc... Bla bla bla mumbo jumbo 因此,我想得到: cookie, recipe, chocolate 如果你能帮我,那就太好了,谢谢,祝你今天愉快 您没有告诉我您使用的是哪种编程语言。下面是一个PHP示
Cookie Recipe for n00bs
#cookie #recipe #chocolate To do this you have to etc...
Bla bla bla mumbo jumbo
因此,我想得到:
cookie, recipe, chocolate
如果你能帮我,那就太好了,谢谢,祝你今天愉快 您没有告诉我您使用的是哪种编程语言。下面是一个PHP示例,它使用与perl兼容的正则表达式:
$text = <<<EOF
Cookie Recipe for n00bs
#cookie #recipe #chocolate To do this you have to etc...
Bla bla bla mumbo jumbo
EOF;
$pattern = '/((?<=#)\w+)/';
preg_match_all($pattern, $text, $matches);
echo implode(', ', $matches[0]);
$text=试试这个:
$re = "/#\\w+/";
$str = "#cookie #recipe #chocolate To do this you have to etc...";
$str .= "#cookie #recipe #chocolate To do this you have to etc...";
preg_match_all($re, $str, $matches);
$r=@implode(", ",@$matches[0]); // for adding comma(,) and space( ).
var_dump( $matches,$r);
array (size=1)
0 =>
array (size=6)
0 => string '#cookie' (length=7)
1 => string '#recipe' (length=7)
2 => string '#chocolate' (length=10)
3 => string '#cookie' (length=7)
4 => string '#recipe' (length=7)
5 => string '#chocolate' (length=10)
string '#cookie, #recipe, #chocolate, #cookie, #recipe, #chocolate' (length=58)
输出:
$re = "/#\\w+/";
$str = "#cookie #recipe #chocolate To do this you have to etc...";
$str .= "#cookie #recipe #chocolate To do this you have to etc...";
preg_match_all($re, $str, $matches);
$r=@implode(", ",@$matches[0]); // for adding comma(,) and space( ).
var_dump( $matches,$r);
array (size=1)
0 =>
array (size=6)
0 => string '#cookie' (length=7)
1 => string '#recipe' (length=7)
2 => string '#chocolate' (length=10)
3 => string '#cookie' (length=7)
4 => string '#recipe' (length=7)
5 => string '#chocolate' (length=10)
string '#cookie, #recipe, #chocolate, #cookie, #recipe, #chocolate' (length=58)
与其删除所有前面没有#
的单词,为什么不尝试匹配前面有#
的单词?您使用什么语言/工具?我尝试在查找和替换上下文中使用它。我正在创建一个键盘Maestro宏,该宏按照查找和替换规则过滤剪贴板。我不知道如何只复制匹配的结果。