CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Searching a string until a certain substring is found

 



perl001
New User

May 12, 2008, 4:18 AM

Post #1 of 3 (3129 views)
Searching a string until a certain substring is found Can't Post

Hello,

I am hoping someone can help me with an ongoing perl problem regarding searching a string until a certain substring is found.

I need to be able to search from the current position and not from the start of the string

An example of the problem:

my $testString = "THIS IS A TEST STRING";

$testString =~ /\G([A-Z]+)/ig # parse THIS

The next thing I need is to search from that point until I find 'TEST'...............

My expected result would be " IS A "

Thanks and Regards,

P.


winfinit
User

May 15, 2008, 10:46 PM

Post #2 of 3 (3017 views)
Re: [perl001] Searching a string until a certain substring is found [In reply to] Can't Post

you can remove all characters that are longer then 3 letters

winfinit:~ winfinit$ cat 1
my $test = "THIS IS A TEST STRING";
$test =~ s/\w{4,}//ig;
print $test . "\n";
winfinit:~ winfinit$ perl 1
IS A

so now you removed all those words, you can do split on a result on a space, and drop all results into an array, like this:

winfinit:~ winfinit$ cat 1
my $test = "THIS IS A TEST STRING";
$test =~ s/\w{4,}//ig;
my @array = split(' ',$test);
foreach(@array){print "$_\n";}
winfinit:~ winfinit$ perl 1
IS
A


hope that helps


KevinR
Veteran


May 15, 2008, 11:05 PM

Post #3 of 3 (3016 views)
Re: [perl001] Searching a string until a certain substring is found [In reply to] Can't Post

$testString =~ /^[A-Z]+([\s\S]+)TEST/;
print $1;
-------------------------------------------------


(This post was edited by KevinR on May 15, 2008, 11:09 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives