java regex split

M

Mani

hi,

i want to write a code to split a line containing names followed
cities,emailids,
and store them separately,

in perl,
if the input file contains,
name1 city1 email1 email2 email3
name2 city2 email1 email2 email3

then,i think,

($name,$city,$email)= split /\s+/,$line,3;

can store the names,cities and emails in separate strings.

In java how can this be done?

-Mani
 
S

Suresh

Mani said:
hi,

i want to write a code to split a line containing names followed
cities,emailids,
and store them separately,

in perl,
if the input file contains,
name1 city1 email1 email2 email3
name2 city2 email1 email2 email3

then,i think,

($name,$city,$email)= split /\s+/,$line,3;

can store the names,cities and emails in separate strings.

In java how can this be done?

-Mani

With Java 1.4 and above you could use the split function of the String
class.

String[] tokens="name1 city1 email1 email2 email3".split(" ");
 
M

Mani

thanks!
Mani said:
hi,

i want to write a code to split a line containing names followed
cities,emailids,
and store them separately,

in perl,
if the input file contains,
name1 city1 email1 email2 email3
name2 city2 email1 email2 email3

then,i think,

($name,$city,$email)= split /\s+/,$line,3;

can store the names,cities and emails in separate strings.

In java how can this be done?

-Mani

With Java 1.4 and above you could use the split function of the String
class.

String[] tokens="name1 city1 email1 email2 email3".split(" ");
 
T

Tim Smith

($name,$city,$email)= split /\s+/,$line,3;

can store the names,cities and emails in separate strings.

In java how can this be done?
....
With Java 1.4 and above you could use the split function of the String
class.

String[] tokens="name1 city1 email1 email2 email3".split(" ");

That's not quite the same thing. Note in the Perl example, he's asked for
it to be split into 3 strings, so he will get these three strings:

"name1"
"city1"
"email1 email2 email3"

Also, probably best to allow for multiple spaces, like in his Perl example,
so what he probably should use is split("\\s+",3).
 
J

Jussi Piitulainen

Tim said:
Suresh wrote: ....
String[] tokens="name1 city1 email1 email2 email3".split(" ");

That's not quite the same thing. Note in the Perl example, he's
asked for it to be split into 3 strings, so he will get these three
strings:

"name1"
"city1"
"email1 email2 email3"

Also, probably best to allow for multiple spaces, like in his Perl
example, so what he probably should use is split("\\s+",3).

That's hardly realistic, if "name1" stands for a real name: some
people have spaces in their names - you and I, for example - and I
thin some cities do, too.

Just wondering what the real problem is.
 
M

Mani

I am not developing any particular application.
I am just learning how certain things in perl can be done using Java.

Just i came across the following link,
http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/

where they have used,
Function to build a hash of hashes
and
Function to build a hash of hashes of hashes

I am just interested and want to know,how it can be done in java.
Thats the intent of my posting...

-Mani


Jussi said:
Tim said:
Suresh wrote: ...
String[] tokens="name1 city1 email1 email2 email3".split(" ");

That's not quite the same thing. Note in the Perl example, he's
asked for it to be split into 3 strings, so he will get these three
strings:

"name1"
"city1"
"email1 email2 email3"

Also, probably best to allow for multiple spaces, like in his Perl
example, so what he probably should use is split("\\s+",3).

That's hardly realistic, if "name1" stands for a real name: some
people have spaces in their names - you and I, for example - and I
thin some cities do, too.

Just wondering what the real problem is.
 
J

Jussi Piitulainen

Mani said:
I am not developing any particular application.
I am just learning how certain things in perl can be done using Java. ....
Thats the intent of my posting...

That's ok then.
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top