0

Here are two examples of the exact same regex pattern matching in Python and R

Python:

In[1]: import re
In[2]: reg = re.compile("oa")
In[3]: files = [".vim", ".jupyter", "Downloads", "Documents", ".bashrc", "R", "Python"]
In[4]: [x for x in files if reg.match(x)]
Out[4]: []

R:

> files <- c(".vim", ".jupyter", "Downloads", "Documents", ".bashrc", "R", "Python")
> grep("oa", files, value = TRUE)
[1] "Downloads"

Why doesn't the Python regex return anything?

Kapil Sharma
  • 1,412
  • 1
  • 15
  • 19
  • Seems Python's **match()** function does a stupid implied anchor `^` at the beginning (but not the end). So, in Python's crippled _re_ engine match function, it takes the liberty of passing `^oa` to the regex engine... Use _search()_ function and this will not happen. You can use _match()_ but you'd have to make it `.*oa` –  May 13 '16 at 14:47
  • Looking at the other SO link, this seems to be a correct approach `In [6]: [x for x in files if reg.search(x)]` `Out[6]: ['Downloads']` – Kapil Sharma May 13 '16 at 14:54
  • 1
    Sometimes other SO links give you _too much/less/different_ information than you're looking for. And a lot of times just wrong information. There really isn't that much _exact_ duplication on SO. –  May 13 '16 at 16:05

0 Answers0