Solved

Regex to strip everything but alphabet characters

Posted on 2010-09-23
8
776 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now