473,568 Members | 3,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegEx - Chk for special chars

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
5 2647
On Apr 29, 6:37 pm, "M Kinnear" <newsgro...@lon yx.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
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
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
On Apr 30, 9:08 am, gosha bine <stereof...@gma il.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
After posting (and less caffeine) I'd realised a few typos.

Cheers

"Toby A Inkster" <us**********@t obyinkster.co.u kwrote in message
news:vs******** ****@ophelia.g5 n.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1865
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to parse the new language, Delphi/Pascal. Source to the highlighter is avail here: http://www.dreamprojections.com/SyntaxHighlighter/Default.aspx ...
5
3481
by: Dianne Siebold | last post by:
I'm not sure if I can use regex for this or how to do it, but here's what I need to do: I want to check that a string contains only the characters A-Z, a-z, 0-9, -(hypen) or _ (underscore). If the string contains any character other than this, return a value. So if a string returns an apostrophe, asterisk, percent sign, etc. it would...
7
2249
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not getting the results I expect. Dim SA as string() Dim S as string S="FBE"
11
3088
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend a hand? import regsub
10
3842
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program is using Python's standard re (regular expressions) module to parse the input file. As Python's routines to read from the file and regular...
7
1673
by: MattMika | last post by:
Can anyone point out the problem with this? The commented regex var and if statement dont work and break the GroupName check when uncommented. I tested the AccessCodeRegxp with preg_match and it seems to work fine, it just wont here. Any pointers would be great. <script language="javascript"><!-- function check_form() { var error = 0; var...
6
3715
by: PaulM | last post by:
...all but the first x chars in a string of arbitrary length? Apologies if this is the wrong forum; I wasn't sure the best place to post about Regex. Background: I am new to Regex for pattern matching. I have started using Yahoo Pipes to manipulate RSS feeds. Yahoo Pipes includes a regex gadget to do string manipulation, but no "stateful"...
0
974
by: Raistlin Majere | last post by:
If there is anything other than: - between one hundred and ten thousand a-z chars - between one hundred and ten thousand A-Z chars or - between one hundred and ten thousand 0-9 chars or - between one hundred and ten thousand " " chars or - between one hundred and ten thousand "," chars or - between one hundred and ten thousand "." chars or...
3
4527
by: rottmanj | last post by:
In my application I get a string from my datasource that I need to replace some of the bad chars in the strings. Right now I have written a pretty standard regex that should replace any of the bad chars in the strings. However, instead of removing the bad chars it removes all chars from the string. The bad chars I am trying to replace are...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.