Ojash
Ojash

Reputation: 1234

Regex to find parts of a string that do not start with a hashtag

I have a string

#this is a #new day at #lf_technology and is #awes0me #Nepal #hattiban peace

Using (#[a-zA-Z0-9_]+) regex, I can extract the hashtags #this, #new, #lf_technology, #awes0me, #Nepal, #hattiban

I need a reg ex to extract is a, day at, and is, peace

Here is what I have used to test http://rubular.com/r/6i9HJUVFFa

Upvotes: 1

Views: 87

Answers (3)

Uri Agassi
Uri Agassi

Reputation: 37419

You can simply look for

/((^| )[a-zA-Z0-9_ ]+)/

http://rubular.com/r/ypEsQY1lhM

For every character except # and _ it would be:

/((^| )[^#_]+)/

http://rubular.com/r/9GhP87HFzn

As the comments below suggest, this will produce results with trailing spaces, so to use it correctly you would need to strip the results:

s.scan(/( [a-zA-Z0-9_ ]+)/).flatten.map(&:strip)
# => ["is a", "day at", "and is", "peace"] 

Upvotes: 3

Rajesh Omanakuttan
Rajesh Omanakuttan

Reputation: 6928

Try with:

/((\s[a-zA-Z]+)+)/

to extract is a, day at, and is, peace.

Ref: http://rubular.com/r/t2TWE7BXzU

Hope it helps :)

Upvotes: 1

aelor
aelor

Reputation: 11116

you can try this :

(?<=\s)\w[^#]*(?!#)

demo here : http://regex101.com/r/sI6uE2

=> irb
=> s = "#this is a #new day at #lf_technology and is #awes0me #Nepal #hattiban peace"
=> s.scan(/(?<=\s)\w[^#]*(?!#)/)
=> ["is a", "day at", "and is", "peace"]

Upvotes: 0

Related Questions