By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

RegEx - Chk for special chars

P: n/a
I want to check a string only contains a-z 0-9 ( ) . and #

I've used

ereg("^[a-zA-z0-9().#]*)$"),$instr)

which parses a string correctly until I try and parse a !,",,$ ... or any
of the other special/reserved characters.

First q: what am I doing wrong
Second q: is there clarification on whether characters need to be escaped
when part of a regex statement? i.e., should it be [$] or [\$] - some
websites/posts state they should be escaped, others say they shouldn't
Thanks

Matt
Apr 29 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Apr 29, 6:37 pm, "M Kinnear" <newsgro...@lonyx.netwrote:
I want to check a string only contains a-z 0-9 ( ) . and #

I've used

ereg("^[a-zA-z0-9().#]*)$"),$instr)

which parses a string correctly until I try and parse a !,",,$ ... or any
of the other special/reserved characters.

First q: what am I doing wrong
I think putting \ before each character should do the trick.
Second q: is there clarification on whether characters need to be escaped
when part of a regex statement? i.e., should it be [$] or [\$] - some
websites/posts state they should be escaped, others say they shouldn't
Have you looked at the PHP manual? Try http://www.php.net/manual/en/referen...ern.syntax.php
>
Thanks

Matt

Apr 30 '07 #2

P: n/a
On 30.04.2007 01:37 M Kinnear wrote:
I want to check a string only contains a-z 0-9 ( ) . and #

I've used

ereg("^[a-zA-z0-9().#]*)$"),$instr)
This line has a syntax error. I'd suggest

preg_match('/^[\w().#]*$/D', $instr)
>
which parses a string correctly until I try and parse a !,",,$ ... or any
of the other special/reserved characters.

First q: what am I doing wrong
Second q: is there clarification on whether characters need to be escaped
when part of a regex statement? i.e., should it be [$] or [\$] - some
websites/posts state they should be escaped, others say they shouldn't
Inside a character class ([...]) only slash \ caret ^ and dash - have
special meaning and should be escaped. All other chars are taken
literally. When using double quotes, you also have to escape dollar sign
when it comes before a letter.

>

Thanks

Matt


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Apr 30 '07 #3

P: n/a
M Kinnear wrote:
I want to check a string only contains a-z 0-9 ( ) . and #

I've used

ereg("^[a-zA-z0-9().#]*)$"),$instr)
Firstly, it looks to me like a couple of extra brackets slipped into the
code you posted, as I don't think the above would parse at all. I'm
assuming what you're actually using is:

ereg("^[a-zA-z0-9().#]*$", $instr)

Firstly, use preg_match. It runs faster that ereg; and ereg is being
phased out in newer versions of PHP. This gives you:

preg_match("/^[a-zA-z0-9().#]*$/", $instr)

Now, the dot (.) in regular expressions has a special meaning. It means
"absolutely any character you want". This means that you are allowing
absolutely any character at all -- that is, your regular expression is
doing nothing. The dot needs to be escaped with a backslash:

preg_match("/^[a-zA-z0-9()\.#]*$/", $instr)

Also, parentheses (these) have a special meaning within regular
expressions, marking out subexpressions. IIRC, this shouldn't be a problem
in your case because that special meaning disappears within square
brackets. However, it doesn't hurt to escape them, and it looks a bit
clearer to people just glancing at the expression:

preg_match("/^[a-zA-z0-9\(\)\.#]*$/", $instr)

Now try it.
Second q: is there clarification on whether characters need to be
escaped when part of a regex statement? i.e., should it be [$] or [\$] -
some websites/posts state they should be escaped, others say they
shouldn't
Generally it never hurts to escape, so when something could go either way,
err on the side of escaping things. Note that there are two levels of
escapes coming into play here -- firstly as you're using "double quotes"
you need to escape against PHP's built in string-parsing. Secondly, you
need to escape against special meanings of characters in regular
expressions.

To make things simpler, it is usually preferable to use 'single quotes',
so that you only have to worry about one set of escaping.

preg_match('/^[a-zA-z0-9\(\)\.#]*$/', $instr)

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 30 '07 #4

P: n/a
On Apr 30, 9:08 am, gosha bine <stereof...@gmail.comwrote:
On 30.04.2007 01:37 M Kinnear wrote:
I want to check a string only contains a-z 0-9 ( ) . and #
I've used
ereg("^[a-zA-z0-9().#]*)$"),$instr)

This line has a syntax error. I'd suggest

preg_match('/^[\w().#]*$/D', $instr)
which parses a string correctly until I try and parse a !,",,$ ... or any
of the other special/reserved characters.
First q: what am I doing wrong
Second q: is there clarification on whether characters need to be escaped
when part of a regex statement? i.e., should it be [$] or [\$] - some
websites/posts state they should be escaped, others say they shouldn't

Inside a character class ([...]) only slash \ caret ^ and dash - have
special meaning and should be escaped. All other chars are taken
literally. When using double quotes, you also have to escape dollar sign
when it comes before a letter.
Thanks
Matt

--
gosha bine

extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok
and one more thing, the OP had
[a-zA-z]
I think he needs [a-zA-Z] as [A-z] allows more than upper and lower
case letters

Apr 30 '07 #5

P: n/a
After posting (and less caffeine) I'd realised a few typos.

Cheers

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:vs************@ophelia.g5n.co.uk...
>M Kinnear wrote:
>I want to check a string only contains a-z 0-9 ( ) . and #

I've used

ereg("^[a-zA-z0-9().#]*)$"),$instr)

Firstly, it looks to me like a couple of extra brackets slipped into the
code you posted, as I don't think the above would parse at all. I'm
assuming what you're actually using is:

ereg("^[a-zA-z0-9().#]*$", $instr)

Firstly, use preg_match. It runs faster that ereg; and ereg is being
phased out in newer versions of PHP. This gives you:

preg_match("/^[a-zA-z0-9().#]*$/", $instr)

Now, the dot (.) in regular expressions has a special meaning. It means
"absolutely any character you want". This means that you are allowing
absolutely any character at all -- that is, your regular expression is
doing nothing. The dot needs to be escaped with a backslash:

preg_match("/^[a-zA-z0-9()\.#]*$/", $instr)

Also, parentheses (these) have a special meaning within regular
expressions, marking out subexpressions. IIRC, this shouldn't be a problem
in your case because that special meaning disappears within square
brackets. However, it doesn't hurt to escape them, and it looks a bit
clearer to people just glancing at the expression:

preg_match("/^[a-zA-z0-9\(\)\.#]*$/", $instr)

Now try it.
>Second q: is there clarification on whether characters need to be
escaped when part of a regex statement? i.e., should it be [$] or [\$] -
some websites/posts state they should be escaped, others say they
shouldn't

Generally it never hurts to escape, so when something could go either way,
err on the side of escaping things. Note that there are two levels of
escapes coming into play here -- firstly as you're using "double quotes"
you need to escape against PHP's built in string-parsing. Secondly, you
need to escape against special meanings of characters in regular
expressions.

To make things simpler, it is usually preferable to use 'single quotes',
so that you only have to worry about one set of escaping.

preg_match('/^[a-zA-z0-9\(\)\.#]*$/', $instr)

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Apr 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.