?
Solved

Identifying first word in string

Posted on 2008-11-12
8
Medium Priority
?
1,271 Views
Last Modified: 2012-05-05
I have a variable containing a string of text.
Whats the easiest way of identifying the first word in the string (i.e. the charachters up to but not including the first space)?
Thanks
0
Comment
Question by:stummj
8 Comments
 
LVL 5

Expert Comment

by:SmartIntel
ID: 22942553
$string=~/([^\s]+)/;
print "$1";
0
 
LVL 13

Expert Comment

by:marchent
ID: 22942570

$str = "test string abcedef";
$str =~ /^\s*(\S+)/;
print "$1\n";

Open in new window

0
 

Author Comment

by:stummj
ID: 22942606
Thanks for your reply.
Im not sure where I plug my varable into your solution though?
The string I'm trying to get the first word of is held in $textString
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Accepted Solution

by:
SmartIntel earned 2000 total points
ID: 22942643
$textString=~/^([^\s]+)/;
$firstString=$1;

Now the first word is stored in $firstString.
0
 

Author Comment

by:stummj
ID: 22942676
Sorry I still dont get it! Isnt that going to change the value of $textString?
0
 
LVL 5

Expert Comment

by:SmartIntel
ID: 22942765
No. It won't change the value of $textString. It will parse the first word from the $textString and stores it in $firstString. You can print it and check.
print "$firstString\n";

0
 

Author Comment

by:stummj
ID: 22942884
Great - that works! Many thanks.
0
 
LVL 85

Expert Comment

by:ozo
ID: 22944865

$textString = "test string abcedef";
print $textString =~ /(\S+)/;
or
print ((split(/\s+/,$textString))[0]);

($firstString)  = $textString =~ /(\S+)/;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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
Suggested Courses

807 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