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:
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.
[COLOR="Lime"] Name: face bob[/COLOR] Email: face3 Phone: face DOB: face /x
[COLOR="Red"] Name: face Email:[/COLOR] face Phone: face DOB: face /x
My current pattern is as such:
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?
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.)
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.
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
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.
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.