472,805 Members | 807 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

regexp to leave only alpha/numeric chars

Using preg_replace() is there a simple regexp to strip everything from a
string except alpha and numeric chars (a-zA-Z0-9)?

$input = "$tring1!";
$pattern =
$input = preg_replace($pattern, "", $input);

result: "tring1"

Jul 17 '05 #1
5 8991
Bosconian wrote:
Using preg_replace() is there a simple regexp to strip everything from a
string except alpha and numeric chars (a-zA-Z0-9)?

$input = "$tring1!";
$pattern =
$input = preg_replace($pattern, "", $input);

result: "tring1"


Chances are anything you will ever try to do with regexp's has been done,
and there may even be a shortcut for it. So at this page:

http://www.php.net/manual/en/referen...ern.syntax.php

you would be able to find this:

$input = preg_replace('\W','',$input)

This will leave in underscores, but that is easily fixed:

$input = preg_replace('[\W_]','',$input)

Also try reading an introduction to regexp's in a Perl guide, you can likely
find something more of a tutorial there to get you started.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #2
"Kenneth Downs" <kn**************@see.sigblock> wrote in message
news:3i************@pluto.downsfam.net...
Bosconian wrote:
Using preg_replace() is there a simple regexp to strip everything from a
string except alpha and numeric chars (a-zA-Z0-9)?

$input = "$tring1!";
$pattern =
$input = preg_replace($pattern, "", $input);

result: "tring1"
Chances are anything you will ever try to do with regexp's has been done,
and there may even be a shortcut for it. So at this page:

http://www.php.net/manual/en/referen...ern.syntax.php

you would be able to find this:

$input = preg_replace('\W','',$input)

This will leave in underscores, but that is easily fixed:

$input = preg_replace('[\W_]','',$input)

Also try reading an introduction to regexp's in a Perl guide, you can

likely find something more of a tutorial there to get you started.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)


Ken,

I was able to use your example, but not without adding starting and ending
delimiters:

'/[\W_]/'

Nothing was stripped without them.

I will bone-up on my regexp syntax.

Many thanks.
Jul 17 '05 #3
Bosconian wrote:
I was able to use your example, but not without adding starting and ending
delimiters: '/[\W_]/'


You might find '/[\W_]+/' faster. Also, on the linked doc page it says
the \w and \W are locale specific; there might be accented characters
left behind. If you don't want that, use '/[^a-z0-9]+/i'.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4
Bosconian wrote:
"Kenneth Downs" <kn**************@see.sigblock> wrote in message
news:3i************@pluto.downsfam.net...
Bosconian wrote:
> Using preg_replace() is there a simple regexp to strip everything from
> a string except alpha and numeric chars (a-zA-Z0-9)?
>
> $input = "$tring1!";
> $pattern =
> $input = preg_replace($pattern, "", $input);
>
> result: "tring1"


Chances are anything you will ever try to do with regexp's has been done,
and there may even be a shortcut for it. So at this page:

http://www.php.net/manual/en/referen...ern.syntax.php

you would be able to find this:

$input = preg_replace('\W','',$input)

This will leave in underscores, but that is easily fixed:

$input = preg_replace('[\W_]','',$input)

Also try reading an introduction to regexp's in a Perl guide, you can

likely
find something more of a tutorial there to get you started.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)


Ken,

I was able to use your example, but not without adding starting and ending
delimiters:

'/[\W_]/'

Nothing was stripped without them.

I will bone-up on my regexp syntax.

Many thanks.


I would love to say I left the delimiters out on purpose to improve your
skills, but it would be a lie. I just forgot.

Glad it helped.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #5
"Ewoud Dronkert" <fi*******@lastname.net.invalid> wrote in message
news:42*********************@dreader4.news.xs4all. nl...
Bosconian wrote:
I was able to use your example, but not without adding starting and ending delimiters: '/[\W_]/'


You might find '/[\W_]+/' faster. Also, on the linked doc page it says
the \w and \W are locale specific; there might be accented characters
left behind. If you don't want that, use '/[^a-z0-9]+/i'.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/


Even better. Thanks for the tip.
Jul 17 '05 #6

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

Similar topics

2
by: entoone | last post by:
I have a field called pword, whenever someone enters anything but numeric, i.e. mixed alpha with numeric, or even just alpha.. the following error appears. Warning: mysql_numrows(): supplied...
0
by: John K. Humkey | last post by:
Ok, it's sed not perl, but it's primarily a regexp question that I've encountered (and failed to understand) before. . . # # When trying to convert a variable number # of spaces separating...
0
by: Ed Leafe | last post by:
I recently upgraded to 4.1 alpha (MySQL 4.1.0-alpha-standard-log) on my Linux server, and came across a problem with a query that had been working in 3.23 that no longer worked in 4.1a. I've...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: success_ny | last post by:
Does anyone have a code snippet to compare those values so I can sort the array of alpha-numeric values that include both characters and integers in it? I.e., if we have values like 4236 and...
6
by: Mark C | last post by:
All, Is there such a function that can strip all non alpha ( not between a-z) characters from a string? I have a function that I currently use that will strip one character at a time from a...
8
by: MLH | last post by:
I have a textbox on a form into which an alpha-numeric string of data is entered. I wish to force the casual user, who would sometimes use upper case, sometimes not and sometimes MIX the case -...
6
by: Edward | last post by:
I need to validate a text box entry, but ONLY if it is 17 characters, otherwise I have to ignore it. My regular expression for the validation is: ^(({9})()()(\d{6}))$ Can I adapt this to...
5
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.