DustyPanda
DustyPanda

Reputation: 81

What does '\t' match to in python regular expression objects?

I was reading through a previous answer to a stackoverflow question (https://stackoverflow.com/a/38162461/11783775), where the '\t' regular expression code was mentioned, i.e. something that matches a tab. For my own curiousity, I wanted to make a regular expression object to see what exactly '\t' matches, i.e. how many spaces, but I have found nothing on my own.

Here is the regex object:

tabTesterRegex = re.compile(r'''
    (\t)
''', re.VERBOSE)

Here is some of the text I was testing with (the bottom line represents a keyboard tab space between 'a' and 'b'.)

text = '''
a b
a  b
a   b
a    b
a     b
a      b
a       b
a        b
a         b
a          b
a           b
a            b
a             b
a              b
a               b
a                b
a   b
'''

Here is the code for finding matches and appending them to a list.

matchList = []
for match in tabTesterRegex.findall(text):
    matchList.append(match)

print(matchList)

The output that I recieve is an empty list, but I would have assumed that '\t' would match to a specific number of spaces.

Thank you

Upvotes: 1

Views: 349

Answers (1)

lvzxy
lvzxy

Reputation: 87

\t is for matching tabs, which are different from spaces. If you append a\t\t\tb, you will get:

a b
a  b
a   b
a    b
a     b
a      b
a       b
a        b
a         b
a          b
a           b
a            b
a             b
a              b
a               b
a                b
a           b

Note that the spacing may vary depending on your IDE's settings. I may be erroneously assuming you're using spaces based on your first few output lines as they don't equate to the length of a tab, but as PyNoob's comment suggests your editor may be converting tabs.

Upvotes: 1

Related Questions