[SOLVED] How do I search for for text between difficult strings, like"yellow"/></w:rP..."
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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:
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.
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.
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:
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.