473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regulgar Expression syntax help...


Folks,

I could do some basic regular expressions (which I picked up from sed,
in unix) but I'm afraid its been a while. I'm wondering if someone
could help out. I basically want to check that a string variable
contains only alphanumeric characters, in addition check for the
underscore. No decimal point check is required, and it should be a case
insensitive check.

I'm using PHP 4.x and would basically like to replace whatever is not
named above with an empty character (thus if a bang or question mark was
in a string, it would be removed from the string).

I know I could use something like preg_replace, but I don't know how to
say "when not found"... In sed, I could do something like

sed -e -i 's/[a-z]|[0-9]|_//g'

This would do the opposite of what I want to do (I think) but I'm lost
when it comes to PHP...

Can someone help me out, via the newsgroup (so all can learn?)

thanks in advance,
randell d.

Jul 22 '05 #1
4 1135
On 2005-07-21, Randell D. <su*****@fiprojects.moc> wrote:
I'm using PHP 4.x and would basically like to replace whatever is not
named above with an empty character (thus if a bang or question mark was
in a string, it would be removed from the string).

I know I could use something like preg_replace, but I don't know how to
say "when not found"... In sed, I could do something like

sed -e -i 's/[a-z]|[0-9]|_//g'


preg_replace("#[^a-z0-9_]#", "", $subject);

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
Jul 22 '05 #2
Tim Van Wassenhove wrote:
#[^a-z0-9_]#


and #2 of the 100 ways to write that expression - probably
*the* most common in c.l.php - involves + optimi[sz]ation
(likely not appreciable though):

#[^a-z0-9_]+#i <- case-insensitive, mind

in at no. 3, the less explicit:

#[^[:alnum:]_]+# //no need for 'i'; can match non-US-ASCIIs

down at number 4, the shorter and even more implicit:

#\W+# //as with 2, can match non-US-ASCIIs, no need for 'i'

they're all different in their own inimitable ways.

--
Jock
Jul 23 '05 #3
John Dunlop wrote:
Tim Van Wassenhove wrote:

#[^a-z0-9_]#

and #2 of the 100 ways to write that expression - probably
*the* most common in c.l.php - involves + optimi[sz]ation
(likely not appreciable though):

#[^a-z0-9_]+#i <- case-insensitive, mind

in at no. 3, the less explicit:

#[^[:alnum:]_]+# //no need for 'i'; can match non-US-ASCIIs

down at number 4, the shorter and even more implicit:

#\W+# //as with 2, can match non-US-ASCIIs, no need for 'i'

they're all different in their own inimitable ways.


Thanks to you both for the suggestions...

My internet access is limited at the moment but I will test this in a
few hours (when I have my LAMP machine)...

However... I'm curious, the solutions provided, when used with
preg_replace, would replace any of those characters, as opposed to
replace anything BUT those characters... (ie I want to replace anything
that is NOT alphanumeric or an underscore with a nospace...)

Thanks again for the help,
Randell D.

Jul 24 '05 #4
Randell D. wrote:
However... I'm curious, the solutions provided, when used with
preg_replace, would replace any of those characters, as opposed to
replace anything BUT those characters... (ie I want to replace anything
that is NOT alphanumeric or an underscore with a nospace...)


That's what they do, for differing values of 'alpha'. The ^
negates the character class, so [^a-z] matches everything
except a, b, c, ... z. [[:alnum:]] in addition to the decimal
digits can cover other letters. \W matches any non-word
character (word = letters, numbers and underscore, where
'letters' depends on your locale settings).

--
Jock
Jul 24 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
8
by: Johnny | last post by:
I need to determine whether a text box contains a value that does not convert to a decimal. If the value does not convert to a decimal, I want to throw a MessageBox to have the user correct the...
15
by: MLH | last post by:
Mr Leigh Purvis gave me a very clever piece of SQL to accomplish what is probably an uncommon objective. In it, he uses the EXISTS operator. I can find no documentation on it in A97 HELP. I would...
7
by: Jeff | last post by:
Hi - For my VB.NET app, I have a SQL2K database that I use to create a dataset with multiple data tables. I've created a dataview (dvReportsTo) of one of the tables, SCPMaster, and I've bound a...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
1
by: teddymeu | last post by:
Hi Guys, trying to bulk insert a csv file into my SQL database from an asp.net vb web app/form page that the user uploads, my problem is that im new to all this and although the SQL statement...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.