Ridhwaan Shakeel
Ridhwaan Shakeel

Reputation: 1053

Getting full matches of newline and tab groups with regex in python

Code is supposed to extract the \n\t groups. It always starts with \n but \t can be 0 or more, with substrings in between them

def longestAbsolutePath(string):
...
paths[path] = r'dir\n\tsubdir1\n\t\tfile1'
special = re.search(r'(\\n(\\t)*)',paths[path])
print special
valid = True
if len(special.groups()) > 1:
    # do something
...
return longest

In the above test string, which is dir\n\tsubdir1\n\t\tfile1, I am expecting to get \n\t and \n\t\t in return.
I have tried re.search and re.findall but was not able to get 2 full matches, because it is returning None and special is printing: AttributeError: 'NoneType' object has no attribute 'groups'.
How can I search the string in question to get 2 expected groups?

Upvotes: 0

Views: 62

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626952

The re.search method will return the first match only, you need to use re.findall or re.finditer. Besides, the pattern is better written with a non-capturing group, (?:...), since you do not use the value afterwards, and it messes up re.findall output if you use this method.

paths[path] = r'dir\n\tsubdir1\n\t\tfile1'
special = re.findall(r'\\n(?:\\t)*', paths[path])
if len(special) > 1:
    # do something

See the Python demo

Upvotes: 1

Related Questions