SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Henry Wong wrote:
The Allowable characters and the number should not be the first character parts can easily be done via the regex. The atleast one Big and Small character case, atleast one number part also be done -- by prepending a bunch of positive look aheads to the regex. One look ahead for each of the three items.
Henry
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Rob Prime wrote:I'd still prefer a loop. Not only will it most likely be faster (untested) but it will probably also be easier to read. Instead of having a long regex that will probably be a bit complex with the lookaheads you see just what is done:
* each character is checked to be valid
* each character is checked against the possible character classes (using a flag per class)
* at the end you validate that enough of these flags are true
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Jothi Shankar Kumar wrote:I managed to get each of the conditions done as a separate pattern so that I can be efficient and faster with the matching. The only thing that I miss is the check for not allowing the number as the first character. How can I do this using Regex??
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
James Sabre wrote:
Jothi Shankar Kumar wrote:I managed to get each of the conditions done as a separate pattern so that I can be efficient and faster with the matching. The only thing that I miss is the check for not allowing the number as the first character. How can I do this using Regex??
Obviously my other response is invisible.
Henry Wong wrote:
James Sabre wrote:
Jothi Shankar Kumar wrote:I managed to get each of the conditions done as a separate pattern so that I can be efficient and faster with the matching. The only thing that I miss is the check for not allowing the number as the first character. How can I do this using Regex??
Obviously my other response is invisible.
As a side note, I am not a fan of providing regex solutions, but won't remove them because it is unlikely that a regex is a home work (or interview) question... but IMHO, people do need to get to the regex pattern themselves. Otherwise, they end up with a pattern that they do not understand.
Henry
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Henry Wong wrote:IMHO, people do need to get to the regex pattern themselves. Otherwise, they end up with a pattern that they do not understand.
Ulf Dittmer wrote:
Henry Wong wrote:IMHO, people do need to get to the regex pattern themselves. Otherwise, they end up with a pattern that they do not understand.
+1. Unless Jothi takes away a solid understanding of the concept of lookahead, this discussion will have been like handing out fish instead of teaching to fish.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
James Sabre wrote:
Rob Prime wrote:I'd still prefer a loop. Not only will it most likely be faster (untested) but it will probably also be easier to read. Instead of having a long regex that will probably be a bit complex with the lookaheads you see just what is done:
* each character is checked to be valid
* each character is checked against the possible character classes (using a flag per class)
* at the end you validate that enough of these flags are true
The desired regex can be constructed so as to be readable and maintainable -
The pattern can be compiled just once and is thread safe.
It is then easy to use -
As to whether or not it is faster than a load of loops - probably not but it is compact and readable and maintainable.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:
If you put it like that then there is no reason to choose one over the other, unless performance is a real issue. (Remember, don't optimize prematurely).
That said, there is one flaw in your regex: your code checks for the presence of an allowed character, but it does not check for the absence of non-allowed characters. That makes the regex a bit more complex; my simple loop still is easy![]()
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
James Sabre wrote:Yep - I rushed. Simple to fix
Blame me, not regex.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:
No offense, but this just proves my point: with a loop, even rushed code would probably be correct. It's just so much easier to make mistakes with regexes.
I once again quote Jamie Zawinski:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rob Prime wrote:
No offense, but this just proves my point: with a loop, even rushed code would probably be correct. It's just so much easier to make mistakes with regexes.
James Sabre wrote:
A predictable response. The error in the regex was due to my misunderstanding of the requirement, not in the regex implementation of what I mistakenly understood to be the requirement. I would have made exactly the same mistake using loops.
James Sabre wrote:
I shall continue to be a regex evangelist but only when it is the right tool for the job. I shall continue preaching the use of the 'fragment' approach when building a regex. Even though obviously outnumbered, in these forums I shall continue expressing my regex views so as to add some balance.
James Sabre wrote:
I agree and don't like just posting code but the general prejudice against regex exhibited in these forums gives the impression that regex are an invention of the devil and should be avoided at all cost....
James Sabre wrote:The OP's problem cries out for regex and is the sort of problem that regex are designed for.
I shall continue to be a regex evangelist but only when it is the right tool for the job.
I shall continue preaching the use of the 'fragment' approach when building a regex.
Even though obviously outnumbered, in these forums I shall continue expressing my regex views so as to add some balance.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|