-
May 22nd, 2009, 03:31 PM
#1
Java RegEx Problem
Hi, I'm having an issue with some string splitting. I have an address "Stewn St 1278 Ste 20". I want to split it on "Ste", or suite, so that I can get the numbers after. However, I DON'T want the 'Ste' from Stewn to be included, as i'll receive "wn" or "st". I only want to split on the phrase 'Ste' alone. Can anyone help me with this? Thanks.
-
May 22nd, 2009, 03:49 PM
#2
Re: Java RegEx Problem
We can help if you ask a clearly worded question. for example:
I want to split it on "Ste", or suite,
What do you mean by 'suite'?
So do you want to split it on Ste or do you just want the numbers?
However, I DON'T want the 'Ste' from Stewn to be included, as i'll receive "wn" or "st".
If you split the string on Ste you will get 'wn st 1278 ' & ' 20'.
If you are struggling to explain what you actually want then show a few example strings and what you want to split them into.
-
May 22nd, 2009, 04:27 PM
#3
Re: Java RegEx Problem
wouldn't it be more wise just to format the string to some defined standard, one space, for example. And then divide it into string blocks and search for the "Ste" block. Finding it would mean that the next one would be the number. And so on.
to trim a string, make it one-spaced and then split into string blocks:
PHP Code:
str = str.trim().replaceAll("\\s+"," "); String[] blocks = str.split(" ");
Last edited by Xeel; May 22nd, 2009 at 04:34 PM.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
May 26th, 2009, 07:41 AM
#4
Re: Java RegEx Problem
roger8989tt,
How about this:
Code:
String[] parts = address.split("\\s+Ste\\s+");
It splits the address at " Ste " (note the white space before and after Ste), leaving two String elements: "Stewn St 1278" and "20".
Or, if you want to include cases with suite spelled in full:
Code:
String[] parts = address.split("\\s+[Ss](ui)?te\\s+");
-
May 26th, 2009, 11:49 AM
#5
Re: Java RegEx Problem
Theoretically interesting but not practical. Normally when parsing strings one won't extract only one piece of data.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
May 26th, 2009, 01:35 PM
#6
Re: Java RegEx Problem
Originally Posted by Xeel
Theoretically interesting but not practical. Normally when parsing strings one won't extract only one piece of data.
yep, but this is the answer to the question in the OP:
Originally Posted by roger8989tt
I want to split it on "Ste", or suite, so that I can get the numbers after. However, I DON'T want the 'Ste' from Stewn to be included, as i'll receive "wn" or "st". I only want to split on the phrase 'Ste' alone. Can anyone help me with this?
-
May 27th, 2009, 07:01 AM
#7
Re: Java RegEx Problem
String[] parts=check.split("\\sSte\\s");
(OR)
String[] parts=check.split(" Ste ");
this is used to split the address at " Ste ".
Here \\s is for space.
U will get the output: 'Stewn st 1278 ' & ' 20'.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|