Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using regular expressions to insert tags

10 views
Skip to first unread message

Sriram Rajagopalan

unread,
Jun 20, 2006, 10:19:18 AM6/20/06
to scr...@perl.org

Hello,

I have a text file in a columnar format. There are seven entries in a row, separated by tabs.

eg.

1       23      555     34      Corporation     Index   Sediment

This has to be tagged as:

<no>1</no><code>23</code><set>555</set><id>34</id><status>Corporation</status><value>Index</value><type>Sediment</type>

I have stored the file contents to $_ and I am using the regex

$_=~s/([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t/<no>$1<\/no><code>$2<\/code><set>$3<\/set><id>$4<\/id><status>$5<\/status><value>$6<\/value><type>$7<\/type>/g;

I am not able to get the text tagged. Can anyone help me out to get my desired codes?

Thanks,

Sriram

Brad Baxter

unread,
Jun 20, 2006, 10:46:04 AM6/20/06
to scr...@perl.org
Your regex is expecting a tab after Sediment.  Is there one?

--
Brad

Terrence Brannon

unread,
Jun 21, 2006, 9:16:32 AM6/21/06
to Sriram Rajagopalan, scr...@perl.org
Why not stick it in a hash and then use XML::Generator?

On 6/20/06, Sriram Rajagopalan <sriram.ra...@ggsinc.com > wrote:

Johan Vromans

unread,
Jun 20, 2006, 11:54:58 AM6/20/06
to Sriram Rajagopalan, scr...@perl.org
Sriram Rajagopalan <sriram.ra...@ggsinc.com> writes:

> 1 23 555 34 Corporation Index Sediment
>
> This has to be tagged as:
> <no>1</no><code>23</code><set>555</set><id>34</id><status>Corporation</statu
> s><value>Index</value><type>Sediment</type>

#!/usr/bin/perl

use strict;
use warnings;

# These are the desired tags.
my @tags = qw(no code set id status value type);

# The input.
my $line = "1 23 555 34 Corporation Index Sediment";

# Split into fields.
my @flds = split(/\t/, $line);

# Process the tags, mapping each with an actual value into a result array, and join.
my $result = join("",
map { "<$_>".shift(@flds)."</$_>"} @tags);

# Show it.
print $result, "\n";

Hapy hacking,

-- Johan

Geoffrey Leach

unread,
Jun 21, 2006, 11:56:13 AM6/21/06
to Sriram Rajagopalan, scr...@perl.org
To assist you in finguring this out, you might try recoding using
s { }{ }/x - and lay out your regex over several lines so as to better
see what's going on. Also, if your data is consistent, (\d+)\s+ would
be lots faster.
0 new messages