Solved

Regex to strip everything but alphabet characters

Posted on 2010-09-23
8
777 Views
Last Modified: 2012-05-10
What's the REGEX to identify all the alphabetic characters out of a string.  For example:
"1abc def2efg4" => "abcdefghij"

Open in new window

I'm testing in Oracle like this but it's just the normal REGEX I'm after.
SELECT regexp_substr('1abc def2ghij4','[A-Za-z]+') from dual

Open in new window

Thanks,

T1 Shopper

0
Comment
Question by:Geoff Millikan
8 Comments
 
LVL 4

Expert Comment

by:rstjean
ID: 33749455
SELECT regexp_replace('1abc def2ghij4','[^A-Z^a-z]+','') from dual
0
 
LVL 2

Expert Comment

by:Jcouls29
ID: 33749464
If you want to keep them separated, simply take off the '+' at the end:

Regex: '[A-Za-z]'

This will return the letters individually.  With the + in there it grabs at least one and will grab as many that are in order as it sees.

Hope this helps.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33751075
What's wrong with your regex?

Have you tried

SELECT regexp_substr('1abc def2ghij4','[^\d]') from dual

This works but doesn't trim result. Which prog language are you using?

Cheers
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
ID: 33753137
If you only want to see the alphabetic characters, then rstjean's pattern would be the way to go--just remove the ^ from the middle. With the second ^ in his/her pattern, your query would also return any ^ within the string.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33753170
What the pattern says is to match any character NOT ( [^] ) an uppercase ( A-Z ) or lowercase ( a-z ) letter. Since you are replacing those matched characters with empty string, the effect is to return a string of only letters.

   
0
 
LVL 4

Accepted Solution

by:
rstjean earned 300 total points
ID: 33753428
marqusG:
What's wrong with your regex?

Have you tried
SELECT regexp_substr('1abc def2ghij4','[^\d]') from dual
This works but doesn't trim result. Which prog language are you using?

The problem with the original regex is that with the substr it will only return up to a space.

Your regex doesn't work either.  As substr returns only a small part of the string.  To return all of the matches as 1 string you need to replace everything that is not a letter with nothing by using replace.

@kaufmed:
Removing the second ^ is correct.   It was a sneaky little bugger that jumped in there.
SELECT regexp_replace('1abc def2^ghij4','[^A-Za-z]+','') from dual
0
 

Author Closing Comment

by:Geoff Millikan
ID: 33755659
Awesome!  Thanks!  (PS.  I've been informed that thankfully there will be no regex in heaven.)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33756141
NP. Glad to help  :)
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question