Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed with regex!

 
Jae Zee
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo ranchers!
I have read a lot of your posts but not been able to decide on whether or not to use regex.

The specification states that:
spec 1: A non-null value in criteria[n] matches any field value that begins with criteria[n].

my solution: I have decided to use startsWith to check this:

spec 2: seacrh for for records where the name and/or location fields exactly match

my solution: not really decided what to use here

I wander if I actually should'nt be using a regex instead of startsWith, but I cant really get a good understanding of what regex does if it only should be used for matching special cases like "*, &" etc or if it should be used as a solution for spec 1 and spec 2.

Id be very very grateful for any help I can get here



thanks
/Jae
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

To keep things short, regular expression (AKA regex) is a mechanism used to match patterns in strings. In your assignment specs, it probably stated that (in the interface section) a record is returned if either one of the following is true:

1) An exact match
2) Starts with the supplied string

Now look here. For a substring match, you only need to be wary of those that starts with the supplied string. As a result, the startsWith() method in the string class is sufficient already. You probably only need regular expressions if the pattern is a little more complex than what Sun expects. Since the only type of pattern you will need to match is if the record data starts with the string supplied by the user, the String class method(s) already suffice.

By the way, in case you are not aware of this, if you have this:



The string "Match" will print. As you can see, the startsWith() method can also perform an exact match. So using this method alone kills 2 birds using 1 stone. It can detect both exact matches and beginning matches.

Hope this helps.
[ March 10, 2005: Message edited by: Liang Anmian ]
 
Jae Zee
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O great thanks for the reply!
Well I think I'll go rigth om then and use startsWith for prefix matching. On the other hand I wander if it really is sufficient to use startswith for exact matching as well. I was thinking of something more in the line of equalsIgnoreCase.

I have now created a separate class that handles criteria matching. Methods in this class are called both by by findByCriteria method in the data class and also from the Model(after the list of prefix matched contractors have been returned to the model). I wander if this is right or if I am way off line. I partly worried because the specification states matching as something done in the findByCriteria method.

Thanks again for the help
/Jae
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to disregard case-sensitivity (which may be a good idea, after all you can't expect the client to give you exact cases), you can create a lowercase version of the record data, as well as a lowercase version of the string supplied by the user. Just use the startsWith() method utilizing both new strings. Of course, you can easily tell that uppercase works as well.

Hope this helps, and good luck with your assignment dude.
[ March 10, 2005: Message edited by: Liang Anmian ]
 
Jae Zee
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic