Parsing Complex Text Structures
By Ian E. Gorman, June 01, 2000
A pattern language that includes recursive patterns and conditional pattern matching can handle complex text structures without supplementary programming. Ian uses the OmniMark pattern language from OmniMark Technologies to do a job that might otherwise be done with tools like lex and yacc.
Jun00: Programmer's Toolchest
#!/bin/perl
# swap.pl
# perl: swap first two fields in a line of tab-delimited fields
# Run this program with the command
# perl swap.pl swap.txt
# where swap.txt is an input file with tab-delimited fields
while (<>) {
s/^([^\t]*)\t([^\t\n]*)/$2\t$1/;
print "$_"
}
Example 2: Example 1 in Perl.