Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 22nd, 2011, 10:11 AM
jenshg jenshg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 5 jenshg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old August 22nd, 2011, 11:11 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,809 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 18 h 6 m 48 sec
Reputation Power: 6112
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.

Reply With Quote
  #3  
Old August 23rd, 2011, 02:59 AM
jenshg jenshg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 5 jenshg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old August 23rd, 2011, 08:46 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,809 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 18 h 6 m 48 sec
Reputation Power: 6112
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

Reply With Quote
  #5  
Old August 23rd, 2011, 09:48 AM
jenshg jenshg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 5 jenshg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old August 23rd, 2011, 10:59 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,711 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 6 h 48 m 5 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
Originally Posted by jenshg
it is inserted in javascript

What code do you have now?

Reply With Quote
  #7  
Old August 24th, 2011, 03:36 AM
jenshg jenshg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 5 jenshg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old August 24th, 2011, 04:00 AM
JClasen JClasen is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2007
Posts: 1,513 JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 7 h 10 m 22 sec
Reputation Power: 1422
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.

Reply With Quote
  #9  
Old August 25th, 2011, 09:35 AM
jenshg jenshg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 5 jenshg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 :-).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Other - Reg exp problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap