0

I'm trying to use the re.match() method to check if my string looks like the regular expression.

In UNIX on the cmd line I'd use "*,*" to check against a string such as, hello,there.

The following python doesn't work, what is there to change?

# line is just some string
if re.match(r'*,*', line):
    # do something
1
  • 1
    The key here is that regular expressions are not the same as shell globs. The shell glob *,* would be .*,.* as a regex. Commented Oct 15 at 17:14

1 Answer 1

2

*,* on UNIX is not a regular expression. In regex, * means to take 0 or more of the preceding character. If you want to check if there is a comma between strings, you can do:

if re.match(r"\w+,\w+"):
    pass

\w specifies a word character, and + specifies one or more. So you are saying I want a comma, between two words.

Sign up to request clarification or add additional context in comments.

2 Comments

Another option would be to point the OP at the fnmatch standard-library Python module, which does implement standard UNIX shell pattern matching.
As a side note, those are called wildcards or globbing patterns. As said, those are note really regular expressions. You may want to look at tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.