Regex to extract numbers/alphabets only

Post Reply
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Regex to extract numbers/alphabets only

Post by hua »

Just wanna share my code that extracts only numbers, alphabets from a string using regex. Irrelevant character will be ignored.
Maybe there is a neater code

Code: Select all | Expand

function cExtractAlphabets(cStr)
  local a_  := hb_regexAll("[A-Za-z ]", cStr), cRet := "", aChar

  if a_ != nil
     for each aChar in a_
         cRet += aChar[1]
     next
  endif
return cRet // returns alphabet and space only
 

Code: Select all | Expand

function cExtractNum(cStr)
  local a_  := hb_regexAll("\d{1,}", cStr), cNum := "", aDigit

  if a_ != nil
     for each aDigit in a_
         cNum += aDigit[1]
     next
  endif
return cNum
 
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
User avatar
Antonio Linares
Site Admin
Posts: 42520
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 75 times
Contact:

Re: Regex to extract numbers/alphabets only

Post by Antonio Linares »

many thanks for sharing it :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply