简体   繁体   English

通过正则表达式提取以#开头的单词(多个空格)

[英]Extract words (multiple whitespace) starting with # by regular expression

I have a problem with my regular expression: 我的正则表达式有问题:

String regex = "(?<=[\\s])#\\w+\\s";

I want a regex that formats a string like this: 我想要一个正则表达式,其格式如下:

"This is a Text     #tag1 #tag2 #tag3"

With the regular expression, I get the last two values as result but not tag1 - because there is more than one whitespace. 使用正则表达式时,我得到的最后两个值是结果,但不是tag1-因为存在多个空格。 But i want all 3 of them! 但是我要他们三个!

I tried some variations, but nothing worked. 我尝试了一些变体,但没有任何效果。

Use this regular expression: 使用以下正则表达式:

(?<=(^|\\S)\\s)#\\w+(?=\\s|$)

Here's a demo. 这是一个演示。

It's a bit unclear from your question what you're really after, so I've put up some simple alternatives: 您的问题还不清楚您要做什么,所以我提出了一些简单的选择:

To capture all the tags in the string, we can use a lookbehind: 为了捕获字符串中的所有标签,我们可以使用一个lookbehind:

((?<=\\s|^)#\\w+)

To capture all the tags at the end of the string, we can use a lookahead: 要捕获字符串末尾的所有标签,我们可以使用超前:

(#\\w+(?=\\s#)|#\\w+$)

If there's always three tags at the end, there's no need for a lookaround: 如果末尾总是有三个标签,则无需环顾四周:

(#\\w+)\s(#\\w+)\s(#\\w+)$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM