Solved

Regex to strip everything but alphabet characters

Posted on 2010-09-23
8
774 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 30

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
 
LVL 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

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 74

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now