Regular Expressions Dilemna

Attempting to parse input into three seperate Strings

Page 1 of 1

1 Replies - 453 Views - Last Post: 10 April 2009 - 01:09 PM Rate Topic: -----

#1 jterry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-January 09

Regular Expressions Dilemna

Post icon  Posted 10 April 2009 - 12:30 PM

Hello,

I am attempting to parse input into three seperate Strings, namely:
String name;
String hull;
String port;

The input file contains submarines with name, hull number, and port i.e.:
USS Hyman G. Rickover [SSN-709] Norfolk, Virginia
USS Jacksonville [SSN-699] Norfolk, Virginia

It should resolve as follows if I do it correctly:
ex. USS Hyman G. Rickover [SSN-709] Norfolk, Virginia
name = USS Hyman G. Rickover
hull = [SSN-709]
port = Norfolk, Virginia

I am attempting the following to capture the name:
if (fileScanner.hasNext("[A-Za-z. ]")){
	name = name + fileScanner.next() + " ";
}



Capturing the hull number
if (fileScanner.hasNext("[[[S][S][N][-0-9]")){
	hull = hull + fileScanner.next() + " ";
}



And since the port should just be whats remaining on the line...
port = fileScanner.nextLine();



I'm sure I'm making some really dumb mistakes due to not understanding how to specify a pattern using characters such as -, ], space, etc.

Thanks for any input!

This post has been edited by jterry: 10 April 2009 - 12:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Regular Expressions Dilemna

#2 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Regular Expressions Dilemna

Posted 10 April 2009 - 01:09 PM

View Postjterry, on 10 Apr, 2009 - 11:30 AM, said:

I am attempting the following to capture the name:
if (fileScanner.hasNext("[A-Za-z. ]")){
	name = name + fileScanner.next() + " ";
}


You probably need to escape the period. The . in regex is any character. A great thing to use for check regexs is http://regexpal.com/ i use it at work all the time. Paste the regex in the top and then the input in the bottom part. It will highlight anything that matches. I haven't used the java regex implementation in awhile or i could help you more. :(


View Postjterry, on 10 Apr, 2009 - 11:30 AM, said:

Hello,

I am attempting to parse input into three seperate Strings, namely:
String name;
String hull;
String port;

The input file contains submarines with name, hull number, and port i.e.:
USS Hyman G. Rickover [SSN-709] Norfolk, Virginia
USS Jacksonville [SSN-699] Norfolk, Virginia

It should resolve as follows if I do it correctly:
ex. USS Hyman G. Rickover [SSN-709] Norfolk, Virginia
name = USS Hyman G. Rickover
hull = [SSN-709]
port = Norfolk, Virginia

I am attempting the following to capture the name:
if (fileScanner.hasNext("[A-Za-z. ]")){
	name = name + fileScanner.next() + " ";
}



Capturing the hull number
if (fileScanner.hasNext("[[[S][S][N][-0-9]")){
	hull = hull + fileScanner.next() + " ";
}



And since the port should just be whats remaining on the line...
port = fileScanner.nextLine();



I'm sure I'm making some really dumb mistakes due to not understanding how to specify a pattern using characters such as -, ], space, etc.

Thanks for any input!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1