Extracting Word from Variable 
Author Message
 Extracting Word from Variable

Quote:

> I have a variable $section that contains the string
> 'store=grocery">Grocery</a>'.  How would I extract the word 'grocery' from
> the last part of this string?  I assume I search for the word store= and
> extract all the characters until I reach " but don't know how to do this.

$line = 'store=grocery">Grocery</a>';
if ($line =~ m/store=([^"]*)"/) {
    print "The item is in $1\n";

Quote:
}

--
Josef M?llers (Pinguinpfleger bei FSC)
        If failure had no penalty success would not be a prize
                                                -- T.  Pratchett


Sat, 15 Oct 2005 07:50:10 GMT  
 Extracting Word from Variable

Quote:

> I have a variable $section that contains the string
> 'store=grocery">Grocery</a>'.  How would I extract the word 'grocery' from
> the last part of this string?  I assume I search for the word store= and
> extract all the characters until I reach " but don't know how to do this.

> Thanks,
> Chris

#--------------------
$string = 'store=grocery">Grocery</a>';
if ($string =~ /store=(.+?)"/) {
  $theword = $1;
Quote:
}

else {
  # didn't find it!
Quote:
}

#--------------------

hth-

--
Michael Budash



Sat, 15 Oct 2005 07:48:10 GMT  
 Extracting Word from Variable

Quote:

> I have a variable $section that contains the string
> 'store=grocery">Grocery</a>'.  How would I extract the word 'grocery'
> from the last part of this string?  I assume I search for the word
> store= and extract all the characters until I reach " but don't know
> how to do this.

Others have answered your immediate question. But it appears you are trying
to parse/extract HTML.
If this is the case then be warned that REs are not the right tool to parse
HTML. You may want to use an HTML parser to parse HTML. It will make your
live and the live of others much easier.

jue



Sat, 15 Oct 2005 12:27:47 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to: Create Regex which extracts N number of words before target word

2. Extract last word on line

3. Extracting a range of words!

4. extracting word from line

5. extract the first word in a string

6. Help please extracting data from a word document

7. Extracting an index from a MS Word 97 doc

8. Extract the nth word from a line

9. Extracting n-grams and words that contain them

10. Need to extract the text from Microsoft Word 6 files

11. Extracting information from Word 7.0 documents.

12. words words words

 

 
Powered by phpBB® Forum Software