• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 788
  • Last Modified:

Regex to strip everything but alphabet characters

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
Geoff Millikan
Asked:
Geoff Millikan
2 Solutions
 
rstjeanCommented:
SELECT regexp_replace('1abc def2ghij4','[^A-Z^a-z]+','') from dual
0
 
Jcouls29Commented:
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
 
Marco GasiFreelancerCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
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
 
käµfm³d 👽Commented:
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
 
rstjeanCommented:
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
 
Geoff MillikanAuthor Commented:
Awesome!  Thanks!  (PS.  I've been informed that thankfully there will be no regex in heaven.)
0
 
käµfm³d 👽Commented:
NP. Glad to help  :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now