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
|
Allowing a slash in a form for half address
Discuss Allowing a slash in a form for half address in the Regex Programming forum on Dev Shed. Allowing a slash in a form for half address 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 23rd, 2012, 11:19 AM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 27
Time spent in forums: 6 h 56 m 13 sec
Reputation Power: 0
|
|
|
Allowing a slash in a form for half address
Hey everyone,
I need to allow half addresses in a form, but need to make sure to limit the use of slashes for injection reasons. The current regex (java) for my address field is:
Code:
^[\\\wá-úÁ-Úà-ùÀ-Ù_\\\-~'\\\.#]+(\\\s[\\\wá-úÁ-Úà-ùÀ-Ù_\\\-~'\\\.#]+)*$
What I was thinking about changing it to is:
Code:
^[\\\wá-úÁ-Úà-ùÀ-Ù_\\\-~'\\\.#(\\\d\\\/\\\d)]+(\\\s[\\\wá-úÁ-Úà-ùÀ-Ù_\\\-~'\\\.#(\\\d\\\/\\\d)]+)*$
I'm not very experienced with regular expressions and was wondering if someone who is would be willing to comment if there's a better way to accomplish this or if this is ok.
|

August 23rd, 2012, 11:51 AM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 27
Time spent in forums: 6 h 56 m 13 sec
Reputation Power: 0
|
|
|
Update: I tried testing that out but unfortunately it won't do. While it allows for slashes, it allows them in places that it shouldn't, such as 666 Candy/and Drive
Stuck just now so any help would be greatly appreciated.
|

August 23rd, 2012, 02:49 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
You're using the character class [...] wrong. It can only be used for single characters (hence the name), not for complete expressions. So the expression "(\d/\d)" will be interpreted as the sequence of "(", "\d", "/", "\d" again and ")".
But I think this is going in the wrong direction, anyway. Never try to filter "dangerous" characters. Instead, use the appropriate escaping function.
This reminds me of a joke about a bank, which had a webform saying something like: Your username mustn't contain the words "DROP", "ALTER" etc.
Apart from that, your pattern á-úÁ-Úà-ùÀ-Ù looks kind of random to me. What is this good for? I mean, there are obviously more "foreign characters" than this.
|

August 23rd, 2012, 02:58 PM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 27
Time spent in forums: 6 h 56 m 13 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Jacques1 Hi,
You're using the character class [...] wrong. It can only be used for single characters (hence the name), not for complete expressions. So the expression "(\d/\d)" will be interpreted as the sequence of "(", "\d", "/", "\d" again and ")".
But I think this is going in the wrong direction, anyway. Never try to filter "dangerous" characters. Instead, use the appropriate escaping function.
This reminds me of a joke about a bank, which had a webform saying something like: Your username mustn't contain the words "DROP", "ALTER" etc.
Apart from that, your pattern á-úÁ-Úà-ùÀ-Ù looks kind of random to me. What is this good for? I mean, there are obviously more "foreign characters" than this. |
I'm actually just working on someone else's code and trying to add the ability to use a slash. The webteam where I work has had so much turnover that there isn't anyone here who worked on the site originally, so I'm hesitant to delve into the back-end java. This regex is actually just defined in a .properties file so I figured it would be easier to just modify it to allow slashes as long as it's in the form "digit slash digit" so that's what I'm going for.
|

August 23rd, 2012, 03:51 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
OK, I see.
But which kind of "injection" do you want to prevent with restricting the slashes?
If there's no security reason for this, I'd just stuff the slash into the character classes. This pattern is nonsense, anyway. For example, I may type in "________", but I'm not allowed to type in "Some street 123" (because of the double space).
Well, if you still insist on your pattern, you could implement it with:
Code:
^(\\\d+/\\\d+|[\\\wá-úÁ-Úà-ùÀ-Ù_\\\-~'\\\.#]+)(\\\s[\\\wá-úÁ-Úà-ùÀ-Ù_\\\-~'\\\.#]+)*$
But be aware that I just corrected your example, if have no idea if this is what you're supposed to do.
|
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
|
|
|
|
|