Results 1 to 3 of 3

Thread: Regex and split

  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Regex and split

    I am trying to use split to split a String of the form
    Java Code:
    "Street Name: bob street, Address: 123 bob street, bar, loblah, 23456, Job Number: 32452"
    I want to split this up and store it in an array as
    Java Code:
    anArr[0] == bob
    anArr[1] == street
    anArr[2] == 123
    anArr[3] == bob
    anArr[4] == street
    anArr[5] == bar
    anArr[6] == loblah
    anArr[7] == 23456
    anArr[8] == 32452
    my thought was
    Java Code:
    String[] anArr = item.toString().split(" |, |Name:|Job|Number:|Street|Address:");
    However, this leaves some blank spaces in array slots, I am going to be using this array to search and Im afraid the incorrect items will be added if there are blank strings in the array, anyone know of any way to split a string by spaces and the other items shown above? Thanks in advance for the help.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    I'm no regex expert but i doubt this can be achieved with a single statement.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    A single statement would be ugly (if at all possible)...seems easier to split on whitespace and remove the items not needed, or use a Pattern/Matcher combo which will allow you to pull out groups into the appropriate array values.

Similar Threads

  1. Regex help?
    By l3ane in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 06:04 AM
  2. How to use split?
    By jeff_limsy in forum New To Java
    Replies: 3
    Last Post: 12-29-2010, 09:23 AM
  3. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •