Splitting a String into a List Using Seperator "*"

Discussion in 'Perl Misc' started by Christopher M. Lusardi, Jan 31, 2004.

  1. 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
    Christopher M. Lusardi, Jan 31, 2004
    #1
    1. Advertising

  2. In article <>,
    Christopher M. Lusardi <> wrote:
    : 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
    --
    Rump-Titty-Titty-Tum-TAH-Tee -- Fritz Lieber
    Walter Roberson, Jan 31, 2004
    #2
    1. Advertising

  3. In article <bvevqa$rlg$>,
    Walter Roberson <-cnrc.gc.ca> wrote:
    |In article <>,
    |Christopher M. Lusardi <> wrote:

    |: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'
    --
    Aleph sub {Aleph sub null} little, Aleph sub {Aleph sub one} little,
    Aleph sub {Aleph sub two} little infinities...
    Walter Roberson, Jan 31, 2004
    #3
  4. Walter Roberson wrote:
    > In article <>,
    > Christopher M. Lusardi <> wrote:
    > : 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
    Chris Mattern, Jan 31, 2004
    #4
  5. Christopher M. Lusardi

    Web Surfer Guest

    In article <>,
    says...
    > 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
    Web Surfer, Feb 5, 2004
    #5
  6. On Fri, 30 Jan 2004 16:13:43 -0800, Christopher M. Lusardi wrote:

    > 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"
    James Willmore, Feb 5, 2004
    #6
  7. Christopher M. Lusardi

    J Krugman Guest

    Here's my entry:

    split /\s*abc\*\s*/

    (string in $_)
    J Krugman, Feb 5, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page