Re: Perl regular expressions help



Rumpa (sgrumpa@xxxxxxxxx) wrote on MMMMCCCLXXVIII September MCMXCIII in
<URL:news:deo51v$26ui$1@xxxxxxxxxxxxxxxx>:
`' Hi,
`' I am trying to learn perl regular expressions.
`' I have:
`' my $store = q(
`' <store>
`' <book>
`' <title>abc</title>
`' <author>xyz</author>
`' </book>
`' <book>
`' <title>pqr</title>
`' <author>mno</author>
`' </book>
`' </store>);
`'
`' I want to extract the titles only in a loop. How can I do this?


That depends on what is a title. For the given example,

/(abc|pqr)/

will do.

/<title>([^<]+)<\/title>/

works for the given example as well, but it might give false positives.


You are probably much better off with a parser, althouh it is
possible to replace a lot of parsers with regular expressions.
Not that any sane person would want to do it.


Abigail
--
#!/opt/perl/bin/perl -w
$\ = $"; $; = $$; END {$: and print $:} $SIG {TERM} = sub {$ := $_}; kill 15 =>
fork and ($; == getppid and exit or wait) foreach qw /Just another Perl Hacker/
.



Relevant Pages

  • Re: when to use {}, "", or nothing, around regular expressions?
    ... looking carefully at all the online documentation, and, it ... but this can't be true for regular expressions? ... REs use a language of their own and this language ... As an example just think of: If the Tcl parser ever sees it it ...
    (comp.lang.tcl)
  • Re: Tokenizing a Regular Expression Literal
    ... proceeding in the parser to know whether you are in an "expression ... context" or an "operator context". ... E4X operators and some newer JavaScript keywords and features need to ... My first attempt was actually based on regular expressions and was ...
    (comp.lang.javascript)
  • Re: Question concerning this list [WebCrawler]
    ... markup languages using regular expressions is a no-no.' ... which are then processed by a grammar-level parser. ... Using regular expressions for LALRparsing is a vice inherited ... character from string" is unreasonably expensive. ...
    (comp.lang.python)
  • Re: Recursive regexps?
    ... The only possible name is Recursive Descent Regular Expressions. ... There are three basic language domains, based on parser power ... the "context-free languages". ... it would be even more accurate to call it "Recursive Descent ...
    (comp.lang.python)
  • Re: Regex is correct but java wont parse it ?
    ... You need to build a tokenizer and a parser. ... The StringTokenizer documentation actually recommends regular expressions be used instead! ... Its even easier to use something like Coco/R, which takes a single input file and generates a Scanner (tokenizer) and a Parser class from it. ... martin@ | Martin Gregorie ...
    (comp.lang.java.programmer)