Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: extracting words with certain characters

by Ratazong (Prior)
on Dec 04, 2012 at 10:47 UTC ( #1007058=note: print w/ replies, xml ) Need Help??


in reply to extracting words with certain characters

The following code may get you started:

my $words = "y____x_z dddetr x_y erre yyy_"; while ($words =~ /([\S]*_[\S]*)/g) { print "$1\n"; };
It looks for any words containing an underscore-character and prints them. Due to the loop and the /g-modifier it will even all words containing an underscore

I intentionally wrote get you started, as there are some basic assumptions inside, e.g.:

  • the "instance names" are seperated by whitespaces (and not commas ...)
  • more than one underscore is OK, and the underscores may follwo each other
  • an instance-name consisting only of underscores is fine
This webpage may help you understanding the regex.

HTH, Rata


Comment on Re: extracting words with certain characters
Download Code
Re^2: extracting words with certain characters
by ColonelPanic (Friar) on Dec 04, 2012 at 11:00 UTC

    Note: this will match "words" containing anything that is not whitespace. This may or may not be what you want. For example, should this be two different words?

    word_1,word_2

    If so, you could use a regex that matches a more traditional definition of a word:

    /(\w*_\w*)/g

    \w matches "word characters": alphanumeric characters plus underscores.



    When's the last time you used duct tape on a duct? --Larry Wall

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007058]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2015-07-05 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls