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!
10 ответов
+ 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
+ 1
Close, but no need to escape the '!' unless it's a special character
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.
0
Thanks so much for your reply! I've tried this on https://regexr.com/ but did not work :(
0
Nah, it still takes words with 'fun' in it, but it's a start!
0
What is the purpose of r at the beginning?
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
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)*")?
0
import re
mystring = "Fun fun! #fun fun* refund multifunction "
pattern = r"(#?[fF]un[*!]?)\s"
print(re.findall(pattern, mystring))
0
Thanks everyone!