Here's the deal. Is there a way to have strings tokenized in a line based on multiple regexes?
One example:
I have to get all href tags, their corresponding text and some other text based on a different regex. So I have 3 expressions and would like to tokenize the line and extract tokens of text matching every expression.
I have actually done this using flex (not to be confused with Adobe), which is an implementation of the good old lex. lex provides an elegant way to do this by executing "actions" based on expressions. One can control the way lex reading a file too (block / line based read).
The problem is that flex actually produces C/ C++ code which actually does the tokenizing job. I have a make file which wraps all these things. I was wondering if perl /python can in some way do the same thing. Its just that I would like to do everything I like in a single programming language itself.
Tokenizing is just one of the things that I want to do as part of my application.
Apart from perl or python can any language (functional also) do this?
I did read about PLY and ANTLR here (Parsing, where can I learn about it).
But is there a way to do it naturally in python itself? pardon my ignorance, but are these tools used in any popular products / services?
Thank you.
Also check out pQuery it as a really nice Perlish way of doing this kind of stuff....
However if your requirement is beyond HTML/Web then here is the earlier "Hello World!" example in Parse::RecDescent...
Probably too much of a large hammer to crack this nut ;-)
If your problem has anything at all to do with web scraping, I recommend looking at Web::Scraper , which provides easy element selection via XPath respectively CSS selectors. I have a (German) talk on Web::Scraper , but if you run it through babelfish or just look at the code samples, that can help you to get a quick overview of the syntax.
Hand-parsing HTML is onerous and won't give you much over using one of the premade HTML parsers. If your HTML is of very limited variation, you can get by by using clever regular expressions, but if you're already breaking out hard-core parser tools, it sounds as if your HTML is far more regular than what is sane to parse with regular expressions.
Sounds like you really just want to parse HTML, I recommend looking at any of the wonderful packages for doing so:
Or! You can use a parser like one of the following:
This example is from the BeautifulSoup Documentation:
If you're specifically after parsing links out of web-pages, then Perl's WWW::Mechanize module will figure things out for you in a very elegant fashion. Here's a sample program that grabs the first page of Stack Overflow and parses out all the links, printing their text and corresponding URLs:
In the main loop, each
$link
is a WWW::Mechanize::Link object, so you're not just constrained to getting the text and URL.All the best,
Paul
From perlop:
Have you looked at PyParsing?
From their homepage:
Here is a program to parse "Hello, World!" (or any greeting of the form ", !"):
The program outputs the following: