How can I match Word Boundary "or" [@ #]?

I can not find a regular expression that matches any hashtag #, @or word boundary. The goal is to break the string into objects and topics like Twitter, in this way:

input = "Hello @world, #ruby anotherString" 
input.scan(entitiesRegex) 
# => ["Hello", "@world", "#ruby", "anotherString"]

To get only words, ex "anotherString", which is too big, is simple:

/\b\w{3,12}\b/

will return ["Hello", "world", "ruby"]. Unfortunately, this does not include hashtags and @s. It seems to work just with:

/[\b@#]\w{3,12}\b/

but returns ["@world", "#ruby"]. This made me realize that word boundaries are by definition not a symbol, so they don’t fall into the category of “One character” and therefore do not match. A few more attempts:

/\b|[@#]\w{3,12}\b/

returns ["", "", "@world", "", "#ruby", "", "", ""].

/((\b|[@#])\w{3,12}\b)/

, [[""], ["@"], ["#"], [""]], , .

/((\b|[@#])\w{3,12}\b)/

. [["Hello", ""], ["@world", "@"], ["#ruby", "#"]]. , , . :

input.scan(/((\b|[@#])\w{3,12}\b)/).collect(&:first)

, collect?

+4
1

/[@#]?\b\w+\b/. , , @ #, ( #ruby # ruby, ) .

p "Hello @world, #ruby anotherString".scan(/[@#]?\b\w+\b/)
# => ["Hello", "@world", "#ruby", "anotherString"]

, , , . #ruby {3,4}:

p "Hello @world, #ruby anotherString".scan(/[@#]?\b\w{3,4}\b/)
# => ["#ruby"]
+4

Source: https://habr.com/ru/post/1533340/


All Articles