Reputation: 25
I want regular expression which will remove hashtags at the end of a string. For example:
*WCW * $14 black vans (size 6)* $8 black fishnet Jacket (size large)* $6 giraffe tank top (size small)* $8 denim shorts (size 2)* $6 red see through purse #platosclosetmooresville #platosclosetlakenorman #resale #gentlyused #preloved #lakenorman #mooresville #MVL #LKN #fashionista #recycleyourstyle #cornelius #davidson #concord #kannapolis #statesville #salisbury #bargainista #lknshopping #mooresvilleshopping #GetCash #SellYourItems #shopping #BargainShopper #ShopLocal #PlatosCloset #giraffe #OOTD #WCW
I have written a regular expression to match the hashtag trail. /#[#\w\s]*\z/
. also tried with /#[#\w\s]*$/u
. But they only return
#lknshopping #mooresvilleshopping #GetCash #SellYourItems #shopping #BargainShopper #ShopLocal #PlatosCloset #giraffe #OOTD #WCW
It should capture all of:
#platosclosetmooresville #platosclosetlakenorman #resale #gentlyused #preloved #lakenorman #mooresville #MVL #LKN #fashionista #recycleyourstyle #cornelius #davidson #concord #kannapolis #statesville #salisbury #bargainista #lknshopping #mooresvilleshopping #GetCash #SellYourItems #shopping #BargainShopper #ShopLocal #PlatosCloset #giraffe #OOTD #WCW
The solution should not match hashtags in the middle of the string.
Upvotes: 0
Views: 59
Reputation: 163467
The regex #[#\w\s]*\z
starts at matching a #
and is followed by a character class which will match one out of several characters. That character class is repeated zero or more times so it would for example also match ###a bc
or just the single starting #
If you want to match hashtags that are as a trail of a sentence and there can not be a word that is not a hashtag in between you could use:
#\w+(?:\s+#\w+)*\z
Explanation
#
Match literally\w+
Match one or more times a word character(?:
Non capturing group
\s+#\w+
match one or more times a whitespace character followed by #
and match one or more times a word character)*
Close non capturing group and repeat zero or more times\z
Assert end of the stringUpvotes: 1