The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Other - Reg exp problem
Discuss Reg exp problem in the Regex Programming forum on Dev Shed. Reg exp problem Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 22nd, 2011, 10:11 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 5
Time spent in forums: 43 m 32 sec
Reputation Power: 0
|
|
|
Other - Reg exp problem
HI,
I can not figure this out... I have tried less advanced reg exp´s before, and
it works fine with digits like first digit need to be a certain number, the lenght are fixed and stuff like that..
But my next task are more complicated and I can´t figure it out.
I need to validate for a mixed input (actually this concern european housenumbers, but only equal ones).
The user should write this housenumber adress in a field for equal numbers (there is another field for the odd numbers).
exampels:
Right input in textbox (only one of the following at a time is considered correct input):
2A
10Z
2
4
110Z
402
402B
402C
Wrong (if the digit is odd, then it is wrong no matter which letter follows):
0
0Z
1A
9
7
7B
109
109A
401
401B
401C
So if the reg exp can figure out if an inputstring is an equal number then optional followed by ONE character from the range of
A-Z. It should not allow '1' or '0', but allow '10'...
The input need to start with a digit. The first possible value is 2, the next 4 and so on. In some cases an adress
will have one letter after it, so 'A', 'B' - 'Z' must be allowed in the end, but only in the end..
Is this task possible with reg exp. ?
Any hint will be very apreciated. Thank You.
regards,
jens
|

August 22nd, 2011, 11:11 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
You're looking for the word even, not "equal." Numbers are even and odd.
Also, as an aside: Zero is even.
This satisfies all your tests:
Code:
/^\d*([2468]|[24680](?<=\d))[A-Z]?$/
-Dan
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

August 23rd, 2011, 02:59 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 5
Time spent in forums: 43 m 32 sec
Reputation Power: 0
|
|
|
looking good
but I can not run the string where I need it and in reg exp testers it generates an error..
It is a bit advanced, so I have not been able to find the error..
/^\d*([2468]|[24680](?<=\d))[A-Z]?$/
Have you runned it successfull in the above form?
Thank you very much for the help already.
regards
Jens
|

August 23rd, 2011, 08:46 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
The following code passes all tests:
PHP Code:
<?php
$tests = explode("\n","2A
10Z
2
4
110Z
402
402B
402C
0
0Z
1A
9
7
7B
109
109A
401
401B
401C");
foreach( $tests as $test )
{
if ( preg_match('/^\d*([2468]|[24680](?<=\d))[A-Z]?$/', $test ) )
{
echo "PASS: $test\n";
}
else
{
echo "FAIL: $test\n";
}
}
It is possible that your language does not support the lookbehind assertion (?<=\d). If that's the case, someone who knows your particular regular expression engine will have to help you.
-Dan
|

August 23rd, 2011, 09:48 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 5
Time spent in forums: 43 m 32 sec
Reputation Power: 0
|
|
|
tried it again...
it looks like it fail with the '?' yes.... it is inserted in javascript. I must look into that and see if I can find a solution.
Thank You for the help again.
regards,
Jens
|

August 23rd, 2011, 10:59 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | Originally Posted by jenshg it is inserted in javascript |
What code do you have now?
|

August 24th, 2011, 03:36 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 5
Time spent in forums: 43 m 32 sec
Reputation Power: 0
|
|
|
javascript
Well, I have not solved it with javascript yet. Have read some sites about javascript mimicking of the "lookbehind assertion" which Dan wrote about. But I have not yet come up with a running solution..
Jens
|

August 24th, 2011, 04:00 AM
|
|
|
|
I can't see, why you would need that assertion at all. /^\d*[02468][A-Z]?$/ should produce exactly the same result for the given input.
// EDIT: I missed the 0 part. To achieve that, one could use probably simply test, if the first letter isn't "0" or change the pattern to sth. along the lines of /^([2468]|\d*[02468])[A-Z]?$/
Regards, Jens
Last edited by JClasen : August 24th, 2011 at 04:06 AM.
|

August 25th, 2011, 09:35 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 5
Time spent in forums: 43 m 32 sec
Reputation Power: 0
|
|
|
perfect
thank you jclasen, exactly what I needed, now it is working perfectly and it turns out that preceeding zeros was not a problem :-).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|