简体   繁体   中英

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. 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:

((?<=\\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+)$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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