Splitting a String into a List Using Seperator "*"

  • Thread starter Christopher M. Lusardi
  • Start date
C

Christopher M. Lusardi

Hello,

How do I split a string using the library function "split" with an asterisk?
I have to tell split break the string when it sees "abc*". I.E.: If I give split
the string "abc def ghi jkl abc* message" I want to get back two parts.
I want "abc def ghi jkl" and "message".

Thank you,
Christopher Lusardi
 
W

Walter Roberson

: How do I split a string using the library function "split" with an asterisk?

Escape it.

:I have to tell split break the string when it sees "abc*". I.E.: If I give split
:the string "abc def ghi jkl abc* message" I want to get back two parts.
:I want "abc def ghi jkl" and "message".

split /abc\*/, $string
 
W

Walter Roberson

|In article <[email protected]>,

|:I have to tell split break the string when it sees "abc*". I.E.: If I give split
|:the string "abc def ghi jkl abc* message" I want to get back two parts.
|:I want "abc def ghi jkl" and "message".

|split /abc\*/, $string

By the way, that split would result in
'abc def ghi jkl ' and ' message' -- notice the trailing and then
leading spaces!

Possibly what you want is

split /\s+abc\*\s+/, $string

but it depends on how you would want to handle
'abc def ghi jklabc* message' or 'abc def ghi jkl abc*message'
 
C

Chris Mattern

Walter said:
: How do I split a string using the library function "split" with an asterisk?

Escape it.

:I have to tell split break the string when it sees "abc*". I.E.: If I give split
:the string "abc def ghi jkl abc* message" I want to get back two parts.
:I want "abc def ghi jkl" and "message".

split /abc\*/, $string

split / abc\* /, $string, otherwise he gets extraneous spaces.

Chris Mattern
 
W

Web Surfer

Hello,

How do I split a string using the library function "split" with an asterisk?
I have to tell split break the string when it sees "abc*". I.E.: If I give split
the string "abc def ghi jkl abc* message" I want to get back two parts.
I want "abc def ghi jkl" and "message".

Thank you,
Christopher Lusardi

my ( @fields , $string );

$string = "abc def ghi jkl abc* message";
@fields = split(/\*/,$string);

# see the command "perldoc -f split" for further details
 
J

James Willmore

How do I split a string using the library function "split" with an asterisk?
I have to tell split break the string when it sees "abc*". I.E.: If I give split
the string "abc def ghi jkl abc* message" I want to get back two parts.
I want "abc def ghi jkl" and "message".

After reading some of the responses and re-reading this post again, I have
to ask ....

What *exactly* are you trying to do? It almost seems like an XY problem.
I say this becasue the last line your your post almost reads like ... you
want to parse a log file ("date ... message" versus "abc efg ... message"
- they *look* similar).

If you're trying to parse a *NIX log file, you could use a module to do
this and make life easier for yourself. Or, you could use 'unpack', which
may work better for you instead of 'split'.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Peace, n.: In international affairs, a period of cheating
<between two periods of fighting. -- Ambrose Bierce, "The
Devil's Dictionary"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top