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: Beginner:
how to extract a string starting from "Name=" in a big string

 



cliffyiu
Novice

May 5, 2012, 1:21 PM

Post #1 of 7 (2696 views)
how to extract a string starting from "Name=" in a big string Can't Post

Hi,

I have a string like

A=1;B=2;C=9;Name=134

I want to extract everything starting from "Name=" and in this case, it should be

Name=134

Anybody knows what I should do?

Thanks

-C


wickedxter
User

May 6, 2012, 9:06 AM

Post #2 of 7 (2240 views)
Re: [cliffyiu] how to extract a string starting from "Name=" in a big string [In reply to] Can't Post


Code
#removed code look below 

#or

use feature qw(say);

my @vars = map { split /;/} $string;
say @vars;


This will split by ; amd save everything inbetween


(This post was edited by wickedxter on May 6, 2012, 4:13 PM)


rovf
Veteran

May 7, 2012, 7:05 AM

Post #3 of 7 (2218 views)
Re: [cliffyiu] how to extract a string starting from "Name=" in a big string [In reply to] Can't Post


Code
my $pos = index($yourstring,"Name="); 
my $result = $pos >= 0 ? substr($yourstring,$pos) : undef;


Or, if you know for sure that your string contains Name=,


Code
my $result = substr(index($yourstring,"Name="));



cliffyiu
Novice

May 7, 2012, 7:32 AM

Post #4 of 7 (2206 views)
Re: [cliffyiu] how to extract a string starting from "Name=" in a big string [In reply to] Can't Post

Thanks very much for your responses!

I just tried another way



Code
@my_new_string = grep(/^Name=/, @my_string);



and it worked as well.


cliffyiu
Novice

May 7, 2012, 7:52 AM

Post #5 of 7 (2195 views)
Re: [cliffyiu] how to extract a string starting from "Name=" in a big string [In reply to] Can't Post

I just got one more question. How should I extract out anything after "Name="?


wickedxter
User

May 7, 2012, 4:40 PM

Post #6 of 7 (2167 views)
Re: [cliffyiu] how to extract a string starting from "Name=" in a big string [In reply to] Can't Post

Add to ur regex
Captures everything after the = till end of the string... The I means incasesensiatave

Code
$string =~ /name=(.*)/i; 
print $1;



rovf
Veteran

May 8, 2012, 1:05 AM

Post #7 of 7 (1911 views)
Re: [cliffyiu] how to extract a string starting from "Name=" in a big string [In reply to] Can't Post

It might work well, but it is completely different from what you asked in your original posting.

 
 


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

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