# Help with regex search

**URL:** <https://discuss.python.org/t/help-with-regex-search/10981>\
**Category:** Python Help\
**Created:** [October 5, 2021, 7:40pm UTC](https://discuss.python.org/t/help-with-regex-search/10981 "2021-10-05T19:40:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bobbie](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bobbie](https://discuss.python.org/u/Bobbie)\
**Post date:** [October 5, 2021, 7:40pm UTC](https://discuss.python.org/t/help-with-regex-search/10981/1 "2021-10-05T19:40:00Z")

</div>

I dont know where to put this so i am putting it here sorry if its wrong but my code is this:

```auto
 Answer = input("Hello "+Human+" I am "+Bot+ "How are you today?")
        HumanEmotion = 0
        import re
        with open ("Feelings.txt", "a") as myfile:
            result = re.search("?",Answer)
            if result != nill:
                myfile.close()
            if result == nill or "None":
                myfile.write(Answer+"\n")
                myfile.close()

```

and the output is

```auto
Hello Dave I am Zelda
How are you today?good?
Traceback (most recent call last):
  File "D:\1Bot\Startup.py", line 54, in <module>
    result = re.search("?",Answer)
  File "D:\lib\re.py", line 201, in search
    return _compile(pattern, flags).search(string)
  File "D:\lib\re.py", line 304, in _compile
    p = sre_compile.compile(pattern, flags)
  File "D:\lib\sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "D:\lib\sre_parse.py", line 948, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "D:\lib\sre_parse.py", line 443, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "D:\lib\sre_parse.py", line 668, in _parse
    raise source.error("nothing to repeat",
re.error: nothing to repeat at position 0

```

What have i done wrong?

---

<div class="post-metadata">

**Author:** ![tjol](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjol/32/3414_2.png) [@tjol](https://discuss.python.org/u/tjol)\
**Post date:** [October 5, 2021, 8:11pm UTC](https://discuss.python.org/t/help-with-regex-search/10981/2 "2021-10-05T20:11:23Z")

</div>

Hi Bobbie,

is there any particular reason you’re trying to use the `re` module? Regular expressions are a powerful tool, but they’re also quite a complex language and often not the best tool for the job — depending on what you’re trying to do.

`"?"` is not a valid regular expression.

If you’re want to check if a certain character or substring is present in a string, the easiest way is to use the `in` operator:

```auto
>>> "?" in "no question marks here"
False
>>> "?" in "Is there a question mark?"
True

```

If you want to know _where_ in a string a particular fixed substring can be found, `str` has a `find` method:

```auto
>>> "Where is the ? here".find("?")
13
>>> "Where is the ? here"[13]
'?'
>>> "no question mark here".find("?")
-1
>>> 

```

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [October 5, 2021, 11:45pm UTC](https://discuss.python.org/t/help-with-regex-search/10981/3 "2021-10-05T23:45:04Z")

</div>

Hi Bobbie,

“?” is not a valid regular expression. Regular expression (“regex”)  
syntax is an extremely compressed, terse mini-programming language, and  
your program “?” is buggy. What you tried to ask the regex engine to do  
is match zero or one of nothing at all, which is impossible.

You can fix the regex by changing it to:

```
re.search(r"\?", Answer)

```

(notice the r is on the _outside_ of the quotes) but that’s just a  
confusing and expensive way of saying:

```
"?" in Answer

```

But it is hard to tell whether or not you should use the regex version  
or not. Do you actually use it for anything?

You can also fix some other problems in your code. Get rid of all the

```
myfile.close()

```

lines, they are not needed when you have “with open(…)” as the “with”  
statement will automatically close the file when you are done.
