Split file line by "|"...

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Split file line by "|"...

webmediauk
Hey Guys,
I've trying to read in a file line by line and split each line into an array by the character "|"
Here's the offending bit of code:

line.split("|").each{
   System.out.println(it)
}

This seems to work for any other character than "|", is it reserved or something?
Many Thanks
Baz
Reply | Threaded
Open this post in threaded view
|

Re: Split file line by "|"...

webmediauk
Solved!

line.split("\\|").each{
                System.out.println(it)
            }

Looks like "|" needs to be escaped with 2 forward slashes.
Reply | Threaded
Open this post in threaded view
|

Re: Split file line by "|"...

John Moore
On 20/12/11 15:46, webmediauk wrote:
> Solved!
>
> line.split("\\|").each{
>                  System.out.println(it)
>              }
>
> Looks like "|" needs to be escaped with 2 forward slashes.

Yes, split expects a regular expression (rather than a simple string),
and | has a special meaning in regular expressions.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Split file line by "|"...

alxndrsn
Would String.tokenize() be more suitable here?  I think it works like
split but doesn't treat the argument as a regex.

I'm a bit confused by the GDK doc for String, as there is no details
of a split() method which takes arguments...
http://groovy.codehaus.org/groovy-jdk/java/lang/String.html


On 20 December 2011 18:55, John Moore <[hidden email]> wrote:

> On 20/12/11 15:46, webmediauk wrote:
>>
>> Solved!
>>
>> line.split("\\|").each{
>>                 System.out.println(it)
>>             }
>>
>> Looks like "|" needs to be escaped with 2 forward slashes.
>
>
> Yes, split expects a regular expression (rather than a simple string), and |
> has a special meaning in regular expressions.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Split file line by "|"...

Ian Roberts
On 21/12/2011 08:25, Alex Anderson wrote:
> Would String.tokenize() be more suitable here?  I think it works like
> split but doesn't treat the argument as a regex.
>
> I'm a bit confused by the GDK doc for String, as there is no details
> of a split() method which takes arguments...
> http://groovy.codehaus.org/groovy-jdk/java/lang/String.html

That's correct, split(String regex) is a standard Java method, not a
Groovy addition.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email