Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove all but characters

Status
Not open for further replies.

dooley28

Programmer
Jan 3, 2008
23
IE
Hi there,

I was just wondering if its possible/easily done to remove all values other than characters from a string?
Ie just leave the words themselves and no other gramatical terms.
 
try

$myString=~s/^\w//g

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I think I need more explanation or an example of what you are trying to do.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
sorry...been out of perl too long. If I understand the question, then this should work:

$myString = "This is my sentence.";
print "$myString\n";
$myString =~ s/\W//g;
print "$myString\n";


Output:

This is my sentence.
Thisismysentence

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Cool, thanks for all the help.

Would there be a way to get a string rip out all the grammer etc, and place each word into an array words.
Ie the sentence: Word1 word2, word3 word4 word5?

Would go to an array
1->Word1
2->word2
3->word3
4->word4
5->word5

Even if I could rip out all the grammar and just have spaces between the words that would suit me grand.
Ie Word1 word2 word3 word4 word5

Thanks again.
 
$myString = "This is a sentence. This is a second sentence, with a comma.";
@myArray = ($myString =~ m/(\w+)/g);
print join(",", @myArray);

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thank you EBGreen that piece of code is perfect.

And thanks to everyone else for their help.
 
Code:
$myString = "This is a sentence. This is a second sentence, with a comma.";
@myArray = split(/\W+/,$myString);
print "$_\n" for @myArray;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Well if you just want it with carriage returns, all you have to do is change:

print join(",", @myArray);

To:

print join("[red]\n[/red]", @myArray);

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I used split() to build the array of words instead of how you did it with a capturing pattern in a regexp. It was just an alternative suggestion.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ooops...you are right. And your way is just as valid. My bad for not looking closer. I thought you were just showing a different way to list the results which I thought was a silly reason for another post.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
not tested, but am wondering based on what Kevin wrote; would there be memory consumption/optimizaiton issues b/w split vs grep?

Code:
$myString = "This is a sentence. This is a second sentence, with a comma.";
@myArray = grep(/\W+/,$myString);
print "$_\n" for @myArray;
 
Considering that your code does not work, the efficiency concern is a bit premature ;)

grep processes a list, not a string. I assume split() is the most efficient way.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I assumed split would be the most efficient way of doing things too and went with that.

Grep seemed a little overkill for what I needed, but thanks for the suggestion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top