Results 1 to 5 of 5

Thread: Regex issue

  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Regex issue

    I need a little help on regex stuff. I am trying to make a program that will hold contact list of information like name email phone and such.
    I am also storing this information into a file in this format:
    Java Code:
    [COLOR="Lime"][1] Name: face bob[/COLOR] Email: face3 Phone: face DOB: face /x
    [COLOR="Red"][2] Name: face Email:[/COLOR] face Phone: face DOB: face /x
    There are probably easier ways of doing what i am doing, but I want to learn more about regex so. This is how it is going.
    My current pattern is as such:
    Java Code:
    \[\d{1,}\]\sName:\s\w+\s(\w+| )
    This selects the number and the first name. the problem is if If they have the option of having just a first name and a last name or just a first name. but the code above if left like so if they don't enter a a last name it will grab Email, which i don't want. What do i need to fix in order to get the right information?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm no regex expert but since you can have different fields set in each record, I question whether regex can do this. (and doubt, if it can do this, whether it's the best tool for the job).

    If the fields always some from the same (limited) set you could store the data in a CSV file. This has the advantage of being editable in many spreadsheet programs.

    If the data in a field could be complex (eg address further broken down into number, street, town etc) then perhaps xml.

    Either way - and there are others like very "free form" field types that can vary from line to line from a possibly unlimited set - I would be looking for someone else's implementation! There are good libraries about for reading/writing csv and xml just a google away.

    (If you are obliged to write your own implementation then csv - or a cut down version of it - might still be worth considering.)

  3. #3
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Yeah, My teacher was also recommended me to use a CSV file but I took on the challenge of doing it this way. if it isn't possible I will just have to try some other way.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If tabs won't appear in the data your file could choose a standard order: name, email, phone,dob, comment. Then your data could look like

    Java Code:
    name here\temail addr\tphone-no\tdate-of-birth\tSome comment
    name here\temail addr\t\tdate-of-birth\tSome comment
    name here\temail addr\tphone-no\tdate-of-birth\t

    The second record (line) doesn't have a phone number and the last doesn't have a comment.

    Reading such a format would involve:

    * read a line
    * use the String split() method to divide it up into its component parts separated by the tab character
    * assign each part to a field of whatever class represents the record as a whole.

    [Edit] I've just realised that this would be a tab separated format. But the principle is the same. Embedded new lines would be a problem (and quotes might confuse a spreadsheet application) but its worth considering.
    Last edited by pbrockway2; 04-20-2011 at 11:25 PM.

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

    Default

    Another option is to make your class implement Serializable. Then you can write your objects out using the ObjectOutputStream and read them back in with ObjectInputStream. The advantage is that you don't have to worry about what order the data is or what format it is. The Streams do all the work for you.

Similar Threads

  1. RegEx
    By watle in forum New To Java
    Replies: 0
    Last Post: 03-22-2011, 08:07 AM
  2. need help with regex
    By o2a1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 12:53 AM
  3. Regex help?
    By l3ane in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 06:04 AM
  4. Using Regex
    By ron87 in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 06:26 AM
  5. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 12:40 AM

Posting Permissions

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