Alok
Alok

Reputation: 10664

Regular expression to check if string has at least one and up to 3 words and more than one hashtag in python

s1 = 'Makeupby Antonia #makeup #makeupartist #makeupdolls #abhcosmetics'
s2 = 'Makeupby Antonia asia #makeup #makeupartist #makeupdolls'
s3 = 'Makeupby Antonia'
s4 = '#makeup #makeupartist #makeupdolls #abhcosmetics'  
s5 = 'Makeupby Antonia asia america #makeup #makeupartist'

Regex should be able to match s1 and s2 only because normal words count is up to 3 and these have more then one hashtag.

I am able to select normal words using \b(?<![#])[\w]+
and
I am able to select hashtag using [#]{1}\w+
but when I combine the expression then it does work.

How can I make final regex using these individual regex which can also track count?

Upvotes: 0

Views: 377

Answers (3)

Paulo Scardine
Paulo Scardine

Reputation: 77399

If I correctly understood your question and if you can assume words are always before tags you can use r'^(\w+ ){1,3}#\w+ #\w+':

for s in ('Makeupby Antonia #makeup #makeupartist #makeupdolls #abhcosmetics',
          'Makeupby Antonia asia #makeup #makeupartist #makeupdolls',
          'Makeupby Antonia',
          '#makeup #makeupartist #makeupdolls #abhcosmetics',  
          'Makeupby Antonia asia america #makeup #makeupartist',):
    print(bool(re.search(r'^(\w+ ){1,3}#\w+ #\w+', s)), s, sep=': ')

This outputs:

True: Makeupby Antonia #makeup #makeupartist #makeupdolls #abhcosmetics
True: Makeupby Antonia asia #makeup #makeupartist #makeupdolls
False: Makeupby Antonia
False: #makeup #makeupartist #makeupdolls #abhcosmetics
False: Makeupby Antonia asia america #makeup #makeupartist

Upvotes: 1

Aran-Fey
Aran-Fey

Reputation: 43366

The sane solution

Split the text into words and count how many of them start with a hash sign.

def check(text):
    words = text.split()

    num_hashtags = sum(word.startswith('#') for word in words)
    num_words = len(words) - num_hashtags

    return 1 <= num_words <= 3 and num_hashtags > 1
>>> [check(text) for text in [s1,s2,s3,s4]]
[True, True, False, False]

The regex solution

import re

def check(text):
    pattern = r'(?=.*\b(?<!#)\w+\b)(?!(?:.*\b(?<!#)\w+\b){4})(?:.*#){2}'
    return bool(re.match(pattern, text))

I'm purposely not going to explain that regex because I don't want you to use it. That feeling of confusion you're probably feeling should be a strong sign that this is bad code.

Upvotes: 4

Calum You
Calum You

Reputation: 15082

Probably a lot of room for optimization (maybe with dependencies/fewer loops) but here's a non-regex solution as discussed in comments:

s_list = [s1, s2, s3, s4]

def hashtag_words(string_list):
    words = [s.split(" ") for s in string_list]
    hashcounts = [["#" in word for word in wordlist].count(True) for wordlist in words]
    normcounts = [len(wordlist) - hashcount for wordlist, hashcount in zip(words, hashcounts)]
    sel_strings = [s for s, h, n in zip(string_list, hashcounts, normcounts) if h>1 if n in (1,2,3)]
    return sel_strings

hashtag_words(s_list)

>['Makeupby Antonia #makeup #makeupartist #makeupdolls #abhcosmetics',
 'Makeupby Antonia asia #makeup #makeupartist #makeupdolls']

Upvotes: 0

Related Questions