Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Re: Splitting OR Regex

    On Thu, 30 Oct 2003 23:37:55 -0500, Scott, Joshua wrote:
    > How can I split the data in a line by a single whitespace but also keep
    > portions between quotes together?
    This is a FAQ:

    perldoc -q delimit


    --
    Tore Aursand <tore@aursand.no>

    Tore Aursand Guest

  2. Similar Questions and Discussions

    1. splitting a PDF file
      Does anyone know if or how to split a multiple page PDF file into single page PDF files in Acrobat 7? I heard this could be done without purchasing...
    2. Splitting a CSV list
      I have the following type of data in a page and I need to be able to pull each and every CN value without the CN or anything behind it. ...
    3. Splitting values
      Hi All... I am really stumped on something I am trying to do. I have a table named games with the following fields: gameID, homeTeam,...
    4. Splitting up an XML File
      I have an XML file that looks like this: <root> <economist publications="true" > <name> <first>John</first> <last>Doe</last> </name>...
    5. splitting an array
      Hi All , I have one array of numbers say (12 17 18 19 120 121 122 123 124 379 480 481). Now I want to get the starting and ending of any...
  3. #2

    Default Re: Splitting OR Regex

    Joshua Scott wrote:
    >
    > How can I split the data in a line by a single whitespace but also keep
    > portions between quotes together? Here is an example of a line of data I'm
    > trying to parse:
    >
    > id=firewall time="2003-10-30 04:15:01" fw=66.74.67.229 pri=5 c=256 m=38
    > msg="ICMP packet dropped" n=63211 src=1.1.1.1 dst=2.2.2.2
    >
    > What I would like to do is keep the data between the quotes together despite
    > the spaces. This is what I'm expecting to get:
    >
    > Id=firewall
    > Time="2003-10-30 04:15:01"
    > Fw=66.74.67.229
    > Msg="ICMP packet dropped"
    > ...etc
    >
    > How should I go about doing this? What I have currently is a regex that
    > splits out the entire line, but certain fields have changed so my original
    > code doesn't work as well.
    Hi Joshua.

    The 'delimited string' FAQ is really for comma-separated values rather
    than the 'name=value' format you have here. It also supports escaped
    quotes within quoted strings, which you probably don't have. The
    regex below should do what you want.

    HTH,

    Rob


    use strict;
    use warnings;

    my $string = q{id=firewall time="2003-10-30 04:15:01" fw=66.74.67.229 pri=5 c=256 m=38 msg="ICMP packet dropped" n=63211 src=1.1.1.1
    dst=2.2.2.2};

    my @fields = $string =~ m/\w+=(?:"[^"]+"|\S+)/g;

    print map "$_\n", @fields;

    *OUTPUT*

    id=firewall
    time="2003-10-30 04:15:01"
    fw=66.74.67.229
    pri=5
    c=256
    m=38
    msg="ICMP packet dropped"
    n=63211
    src=1.1.1.1
    dst=2.2.2.2


    Rob Dixon Guest

  4. #3

    Default Re: Splitting OR Regex

    Rob Dixon wrote:
    > my @fields = $string =~ m/\w+=(?:"[^"]+"|\S+)/g;
    Nice!!

    Joseph

    R. Joseph Newton Guest

  5. #4

    Default Re: Splitting OR Regex


    R. Joseph Newton wrote:
    >
    > Rob Dixon wrote:
    >
    > > my @fields = $string =~ m/\w+=(?:"[^"]+"|\S+)/g;
    >
    > Nice!!
    <blush> Thanks! </blush>

    Rob


    Rob Dixon Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139