Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Kanchan Yadav

Greenhorn
+ Follow
since Nov 30, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kanchan Yadav

Hello Vinoth, just one query. I want to set this class as an input verifier for a text field on one of my Swing forms. Which would be a better way to code it? 1. Using the 'match' method (as above) or using 'Pattern' & 'Matcher' classes. The code for pre-compiled pattern is:



I have tried testing your regex with both the code samples & there are some differences in output.
1. Using the 'match' method returns 'false' with most of the input strings mentioned above.

2. Using pattern class does match the input strings but the 'matcher groups' are not the same as input strings. For eg. A.B. Williams would give the following output:

1. "String is A.B. Williams Match result is false"
2. "String is A.B. Williams Match result is true"


In another detail display, it gives
Matcher grp is: A Matcher start is: 0 Matcher end is: 1
Matcher grp is: B Matcher start is: 2 Matcher end is: 3
Matcher grp is: Williams Matcher start is: 5 Matcher end is: 13

(I understand that because of the use of ?:, we do not get the characters "." & " " as a mtching group in the result.)

My question is why do the two cases treat the input strings differently. I'm not sure but could it be because 'match' does a complete match & would not settle for anything less whereas 'pattern' returns true even if a 'minimum' match is found?
Can you suggest how it is generally done & which would be a better way given the context?

Thanks for your help.
10 years ago
Thanks Vinoth. I tried that regex & yes it does work in most of the situations.

Thanks Matthew. I went through that article. Yes, I would definitely need to keep in mind all those aspects & 'localization' etc. for later. Presently, I can continue with the min. of requirements that I've mentioned.

Thanks Ulf. Infact the complicated regex is a result of my trying to get all things done & dealing with all conditions @ once.

Thanks All for guiding me. I'm reading a few tutorials on 'regex' & trying to come up with a good one for this. Greenhorn that I am its taking me a little more time to figure out . But yes please do watch this post; I'm sure to come up with something soon. I'm at it.
10 years ago
Thanks Matthew & Ulf.. My first post got some replies
Matthew - Actually, I tried with a simpler one but as I kept adding those conditions it got complicated. I'll try this one & let you know.

Ulf - That's definitely something new to learn & I will use it in my code.

By the way, I can not yet understand the use of '?' and '^' in regex. I'm trying to read more about them, but still clueless as to their application in my code. If you can suggest some good tutorial/link/URL.. please do so.

Thanks!
10 years ago
And yes I have tried giving it the same inputs as mentioned above, but it doesn't seem to work. Thanks.
10 years ago
Hi! I have been reading some posts & links on 'Regex'. I need to write one that checks first & last names. I know many are available on the web but just wanted to try on my own...
This regex should match names beginning with Capital, with dots & spaces, with atleast one multi-length string as in 'Java' & of length 45 chars.
It should also be able to match names like - A.B. Henry, A B Henry, A Henry, Alex H, Alex H., Alex Henry, Alex Williams Henry, etc.


I've created a package 'validator' in my application & named this class as 'FirstLastNameVerifier'. The code goes like this:

Thanks for your help.
10 years ago