LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2010, 03:17 PM   #1
dsayars
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Rep: Reputation: 0
How do I search for for text between difficult strings, like"yellow"/></w:rP..."


I am trying to capture and change text between two keyword strings in Perl, but the keyword strings are fragments of XML, mostly non-word characters. Thanks to replies to earlier posts, I managed to do it using real words, like "start" and "end" For example, this works.

while ($text=~ s/\bstart\b (.*?) \bend\b//) {
print $1, "\n";
$term = "term_$1_term";
}

However, my actual start keyword string is "yellow"/></w:rPr><w:t>VAR</w:t></w:r>" and the closing string is "<". Using lots of escape characters, I can find the two keywords like this:

$text=~m/yellow\"\/\>\<\/w:rPr\>\<w:t\>/;
$text=~m/\>/;

But substituting these strings for "start" and "end" in the while statement (and removing the \b character) doesn't work. In other words, this doesn't work:

while ($text=~s /yellow\"\/\>\<\/w:rPr\>\<w:t\>(.*?) \<//) {
print $1, "\n";
$term = "term_$1_term";
}

Can anyone tell me how to put these resistant strings into the while statement?
 
Old 07-21-2010, 05:18 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The number of meta-characters to quote is large, and difficult to read. Consider something like (untested):
Code:
    my $regexStart = quotemeta '"yellow"/></w:rPr><w:t>VAR</w:t></w:r>"';  # not sure which double-quotes you actually need, here
    my $regexEnd   = quotemeta '"<"';

    while( $text =~ s/$regexStart(.*?)$regexEnd// ){
        print $1,"\n";
        my $term = "term_$1_term";
    }
Also, I'm not sure your regex '(.*?)' really does what you want (because I'm not sure what you are trying to match). Perhaps you really want '(.+)' I can't actually think of how three meta-characters like .*? matches anything.

--- rod
 
Old 07-21-2010, 06:39 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dsayars View Post
I am trying to capture and change text between two keyword strings in Perl, but the keyword strings are fragments of XML, mostly non-word characters. Thanks to replies to earlier posts, I managed to do it using real words, like "start" and "end" For example, this works.

while ($text=~ s/\bstart\b (.*?) \bend\b//) {
print $1, "\n";
$term = "term_$1_term";
}

However, my actual start keyword string is "yellow"/></w:rPr><w:t>VAR</w:t></w:r>" and the closing string is "<". Using lots of escape characters, I can find the two keywords like this:

$text=~m/yellow\"\/\>\<\/w:rPr\>\<w:t\>/;
$text=~m/\>/;

But substituting these strings for "start" and "end" in the while statement (and removing the \b character) doesn't work. In other words, this doesn't work:

while ($text=~s /yellow\"\/\>\<\/w:rPr\>\<w:t\>(.*?) \<//) {
print $1, "\n";
$term = "term_$1_term";
}

Can anyone tell me how to put these resistant strings into the while statement?
I don't remember - have I already given the http://docstore.mik.ua/orelly/perl/cookbook/ch06_09.htm link ?

Anyway, if you are parsing HTML - just don't. I.e. use an existing parser: http://search.cpan.org/search?query=...arser&mode=all .

And, I think, I've already given http://search.cpan.org/~adamk/Text-B...xt/Balanced.pm - this module is a generic solution for whatever BEGIN .. END markers - not just plain words
 
1 members found this post helpful.
Old 07-22-2010, 01:13 AM   #4
dsayars
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
The number of meta-characters to quote is large, and difficult to read. Consider something like (untested):
Code:
    my $regexStart = quotemeta '"yellow"/></w:rPr><w:t>VAR</w:t></w:r>"';  # not sure which double-quotes you actually need, here
    my $regexEnd   = quotemeta '"<"';

    while( $text =~ s/$regexStart(.*?)$regexEnd// ){
        print $1,"\n";
        my $term = "term_$1_term";
    }
Also, I'm not sure your regex '(.*?)' really does what you want (because I'm not sure what you are trying to match). Perhaps you really want '(.+)' I can't actually think of how three meta-characters like .*? matches anything.

--- rod
Thanks. Problem now solved (this one at least) as per my update to my original question. Regarding (.*?), I got these from a general solution I found in another thread on this site, which was

while ($text=~ s/\bstart\b (.*?) \bend\b//)

Don't have a lot of insight into why this works, but it does.
 
Old 07-22-2010, 01:19 AM   #5
dsayars
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
I don't remember - have I already given the http://docstore.mik.ua/orelly/perl/cookbook/ch06_09.htm link ?

Anyway, if you are parsing HTML - just don't. I.e. use an existing parser: http://search.cpan.org/search?query=...arser&mode=all .

And, I think, I've already given http://search.cpan.org/~adamk/Text-B...xt/Balanced.pm - this module is a generic solution for whatever BEGIN .. END markers - not just plain words
Thanks. I got confused by the references you gave me before, on my earlier post. This is Word XML, not HTML, but I'll see if these modules help.
 
Old 07-22-2010, 01:30 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dsayars View Post
Thanks. I got confused by the references you gave me before, on my earlier post. This is Word XML, not HTML, but I'll see if these modules help.
http://search.cpan.org/search?query=xml+parser&mode=all - just don't reinvent the wheel.
 
  


Reply

Tags
perl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
Apply "Advanced Search" options to "My LQ" searches PTrenholme LQ Suggestions & Feedback 22 03-10-2007 08:30 AM
"setup" or "elinks" text garbage displayed sr99 Red Hat 1 12-06-2006 07:43 PM
"Search this forum" text box maxfacta LQ Suggestions & Feedback 6 08-01-2005 12:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration