Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to receive regex from command line in python

I want to receive a delimiter like '\t' (tab) from command line, and use it to parse a text file.

If I put

delimiter = sys.argv[1]

in the code, and type from the command line

$ python mycode.py "\t"

delimiter is '\\t' i.e., python does its thing to preserve input string as is.

I want to convert this to '\t' so that I can use e.g.,

'a\tb\tc'.split(delimiter)

to get ['a','b','c'].

I've tried to convert '\' to '\', but failed.

Is there a built-in python function to handle regex from the command line?

like image 927
pyrookie Avatar asked Oct 20 '12 14:10

pyrookie


People also ask

How does Python support regex?

The Python module re provides full support for Perl-like regular expressions in Python. The re module raises the exception re. error if an error occurs while compiling or using a regular expression. We would cover two important functions, which would be used to handle regular expressions.

Can you do regex in Python?

Python, Java, and Perl all support regex functionality, as do most Unix tools and many text editors.

What is regex command?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.


1 Answers

In Python 2 you can use str.decode('string_escape'):

>>> '\\t'.decode('string_escape')
'\t'

In Python 3 you have to encode the string to bytes first and then use unicode_escape:

>>> '\\t'.encode().decode('unicode_escape')
'\t'

Both solutions accept any escape sequence and will decode them correctly, so you could even use some fancy unicode stuff:

>>> '\\t\\n\\u2665'.encode().decode('unicode_escape')
'\t\n♥'
like image 88
poke Avatar answered Oct 05 '22 22:10

poke