Regex question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Regex question

I'm new to regex and need some help. Trying to screen a word a from document. It can begin with or without #, and can end with a symbol like * or ! As an example, the word fun. It should include: Fun, fun, #fun, fun* but should exclude words that has fun in it, like refund, multifunction etc. This is what I used: (#|)fun($|[^A-Za-z]) It does all of the above but not uppercase Fun. Any suggestions?? Thanks so much in advance!

22nd Nov 2020, 1:52 PM
GG128
10 Answers
+ 1
The purpose is to make the string a raw string. If I didn't do that, I would have to escape , for example, '\s' twice so it would be '\\s' and putting an 'r' at the beggining is way easier
22nd Nov 2020, 2:24 PM
Slick
Slick - avatar
+ 1
Close, but no need to escape the '!' unless it's a special character
22nd Nov 2020, 3:11 PM
Slick
Slick - avatar
0
try: fun_regex = re.compile(r"#*(F|f)un(\*|!)*") It searches for the words fun or Fun which may or may not start with '#' and may or may not end with '*' or '!'. EDIT: will also pull '*' and '!' if both are availible in the search Notice how the check for '*' at the end has to be escaped.
22nd Nov 2020, 2:15 PM
Slick
Slick - avatar
0
Thanks so much for your reply! I've tried this on https://regexr.com/ but did not work :(
22nd Nov 2020, 2:20 PM
GG128
0
Nah, it still takes words with 'fun' in it, but it's a start!
22nd Nov 2020, 2:21 PM
Slick
Slick - avatar
0
What is the purpose of r at the beginning?
22nd Nov 2020, 2:22 PM
GG128
0
regex = re.compile(r"\s#*(F|f)un(\*|!|\s)*") This accounts for the space that should be before "fun" and has room for an optional space at the end
22nd Nov 2020, 2:23 PM
Slick
Slick - avatar
0
Thanks again. I still can't get it to work at https://regexr.com/ before I use in my program (Java - reading from a file and then screening). Just curious though, if I only care for ! at the end (not ! or *), is it then regex = re.compile(r"\s#(F|f)un(\!|\s)*")?
22nd Nov 2020, 2:30 PM
GG128
0
import re mystring = "Fun fun! #fun fun* refund multifunction " pattern = r"(#?[fF]un[*!]?)\s" print(re.findall(pattern, mystring))
22nd Nov 2020, 4:56 PM
rodwynnejones
rodwynnejones - avatar
0
Thanks everyone!
22nd Nov 2020, 5:47 PM
GG128