FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Regex to extract numbers/alphabets only
Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Regex to extract numbers/alphabets only
Posted: Tue Jan 17, 2023 04:19 AM
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 (fw): Select all Collapse
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 (fw): Select all Collapse
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
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Regex to extract numbers/alphabets only
Posted: Tue Jan 17, 2023 08:12 AM
many thanks for sharing it :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com

Continue the discussion