Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Silly Parse String Question!

Posted on 1999-07-02
11
Medium Priority
?
206 Views
Last Modified: 2010-03-04
What I am trying to do is take: $string = 'my little red
wagon'

and return just: $string = 'my little'

In other words, just parse the first two words of the string!

Thanks!

0
Comment
Question by:EasyRider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
11 Comments
 
LVL 2

Expert Comment

by:dmethvin
ID: 1213703
Assuming you want the first two space-separated hunks of characters (e.g., "my_wierd$100-gizmo w0rkz" is two words):

my $string = 'my little red wagon';
my ($tw) = $string =~ /^(\S+\s\S+)/;
print $tw;

I don't know what you want to do in the case that there aren't two words in the string. You could handle that with an or-clause that had the default value you wanted to return:

my $string = 'wagon';
my ($tw) = ($string =~ /^(\S+\s\S+)/)[0] || 'two words';
print $tw;



0
 
LVL 2

Expert Comment

by:stam061398
ID: 1213704
I think this is more simple :

$string = 'my little red wagon';
@words = split(" ",$string);
$new_string = join(" ",@words[0,1]);


0
 

Expert Comment

by:Hugh_Jerpenis
ID: 1213705
Don't use \s or \S for this; you don't want spaces, you want word boundaries - \b. So:

$string=~ /^(.*\b.*)\b/; # match anything up to the second $string = $1 if $1; # save the start of it, if we had a match

If the regexp doesn't match at all, $string won't be touched (because of the if-statement).

This should do the trick...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:dmethvin
ID: 1213706
Hugh, I don't think you tested your solution. (Hint: .* is greedy.) I agree, though, that \b would be good to use if you want to count words as something other than space-separated entities.
0
 

Author Comment

by:EasyRider
ID: 1213707
Yikes! My code complaining it is not initialized and it returns nothing :(  If it matters, $string is user defined so I do not have to define it at all.
0
 

Author Comment

by:EasyRider
ID: 1213708
dme+, I think I know why your code isn't working for me, since I am using it via a web script I and using URI which is escaping the string! So, actually instead of spaces there is +'s. Gawd I feel stupied.
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1213709
$string="This is a string";
if ($string=~/^\b/) {
    print join(" ",(split(/\b/,$string))[0,2]),"\n";
} else {
    print join(" ",(split(/\b/,$string))[1,3]),"\n";
}
$string="+++++This+is+a+string";
if ($string=~/^\b/) {
    print join(" ",(split(/\b/,$string))[0,2]),"\n";
} else {
    print join(" ",(split(/\b/,$string))[1,3]),"\n";
}

0
 
LVL 84

Expert Comment

by:ozo
ID: 1213710
print $string=~/(\w+\W+\w+)/;
$string=~s/\W*(\w+\W+\w+).*/$1/;
0
 
LVL 8

Accepted Solution

by:
jhurst earned 200 total points
ID: 1213711
$string="my little waggon";
@x=split(" ",$x); #each word as a separate entry
$string="$x[0] $x[1]"; #make a string of the first two with a space between
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1213712
jhurst's code won't work :-)
0
 
LVL 2

Expert Comment

by:stam061398
ID: 1213713
Hey!
That was my answer!
;-)

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question