Regex to extract numbers/alphabets only

Regex to extract numbers/alphabets only

Postby hua » Tue Jan 17, 2023 4: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: Select all  Expand view

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 view

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
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: Regex to extract numbers/alphabets only

Postby Antonio Linares » Tue Jan 17, 2023 8:12 am

many thanks for sharing it :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 71 guests