3 Replies - 190 Views - Last Post: 14 June 2012 - 05:37 PM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

String splitting

Posted 14 June 2012 - 02:43 PM

Hi everybody,
I have a string that's set up as follows:
text, which may or may not have spaces or numbers in it [space]number1[space]number2[space]number3



So basically, it's lots of random text, followed by a space, followed by three numbers separated by spaces. A simpler version would be:

text number1 number2 number3



There's never any leading or trailing whitespace because I trim the string first.

I'd like to split the string into the text and the three numbers, but I'm not sure how to start with the regex. I know what I can't do:
1. I can't split on spaces, because the text might have spaces.
2. I can't find the first instance of a number and use substring because there might be numbers in the text.
3. I can't find the last instance of a character because the text might end with a number.

Could anyone give me some ideas for the regex? I do know how to use the regexes, but since I'm not sure how to approach this problem in the first place I'm kind of stuck on the regex.

I'd appreciate any advice.

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: String splitting

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 209
  • Joined: 19-December 11

Re: String splitting

Posted 14 June 2012 - 04:00 PM

Turn it into a char array, remove the numbers and store them in some other variable and then construct a new String with your char array.
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,204
  • Joined: 05-April 11

Re: String splitting

Posted 14 June 2012 - 04:56 PM

You can use split, you will just have to put the String back together again using a StringBuilder/buffer

I would have liked if the lastIndexOf would accept a regex, but since it doesn't, I had to use a Matcher.
The tricky part is that there are two groups notated by ( ). I use the groups to find out the last occurrence of the three numbers. Group 0 will always be the whole String supplied.

		String text = "Some text 123 with numbers 123 456 1";
		
		Pattern pattern = Pattern.compile("(.*)( \\d+ \\d+ \\d+)");
		
		Matcher matcher = pattern.matcher(text);
		
		if (matcher.find()) { //Find matches
			String theText = matcher.group(1);
			String numbers = matcher.group(2);
			
			System.out.println("Text: " + theText);
			System.out.println("Numbers: " + numbers);
		}


Was This Post Helpful? 1
  • +
  • -

#4 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: String splitting

Posted 14 June 2012 - 05:37 PM

Thank you both,
Those are both great possible solutions, and I'm sure I could make at least one of them work.
Thanks again!
Zel2008
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1