Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives for regex in Python

Regular expressions are highly unreadable and difficult to debug. Does there exist any replacement for text processing which could be handled by mere mortals?

Criteria include

  • It's a library or a tool (please point the answer to the library itself)

  • Human readable syntax (no cheatsheets needed)

  • Documentation with examples

  • Able to debug expressions

If possible can you mention language specific and language independent solutions. I am mainly developing on Python, but I'd hope to see a library which could be ported to other languages/platforms.

I once read that Haskell would have nice text processing capabilities, but again, this is a built-in language solution, not a generic solution.

Edit: Please do not give answers "regular expressions are not bad, do like this!" Stackoverflow.com is not a place for subjective opinions, but I think a regular expressions are bad and I want to see my alternative options for using them.

like image 965
Mikko Ohtamaa Avatar asked Jul 16 '11 11:07

Mikko Ohtamaa


People also ask

What is replace regex Python?

To replace a string in Python using regex(regular expression), use the regex sub() method. The re. sub() is a built-in Python method that accepts five arguments maximum and returns replaced string.

Is regex faster than string replace Python?

Two-regex is 10 times slower than string replacement, and regex/lambda 25 times slower.

Is regex important in Python?

Regex is very useful in searching through large texts, emails, and documents. Regex is also called “programming language for the string matching”. Before diving into the regular expressions and their implementation in python, it is important to know their applications in the real world.


2 Answers

I know this post was old, but people might be benefit from this question/answers. VerbalExpressions is still using regex behind the scene, but in a friendly way.

Intro: http://thechangelog.com/stop-writing-regular-expressions-express-them-with-verbal-expressions/ Python fork: https://github.com/VerbalExpressions

like image 160
wanghq Avatar answered Sep 30 '22 15:09

wanghq


pyparsing offers another method to create and execute (simple) grammars. I've been using it in a project for parsing different kind of log files and the use was rather simple and somewhat more intuitive than with regexps.

like image 40
Nailor Avatar answered Sep 30 '22 17:09

Nailor