CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2009
    Posts
    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.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.

  3. #3
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    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!

  4. #4
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    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+");

  5. #5
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    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!

  6. #6
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Java RegEx Problem

    Quote Originally Posted by Xeel View Post
    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:

    Quote Originally Posted by roger8989tt View Post
    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?

  7. #7
    Join Date
    May 2009
    Posts
    5

    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
  •  





Click Here to Expand Forum to Full Width

Featured