Regular Expressions - Petewg/harbour-core GitHub Wiki
🔙 Home
Note: functions listed below, came from Juan Gamero's site and are cited here to be used as a quick reference. Should you be interested for an extensive documentation about Harbour Regular Expressions, then it's preferable (and strongly recommended) to visit his page! You can, also, take a look at this Regular Expressions quick reference. And here is another one, quite good, List of Regular Expressions
-
hb_AtX(
<cRegEx>, <cString>, [<lCaseSensitive>], [@<nStart>], [@<nEnd>]) ➜ cFoundString|NIL
Searches into<cString>string for a sub-string matching with<cRegEx>regular expression. Returns the first sub-string that matches the regex pattern, orNILif no match found. The range of search can be limited between<nStart>and<nEnd>positions inside<cString>(defaults are1and length of cString); if those params (or any of them) are passed by reference, they will hold the starting point of the finding and/or its length. -
hb_RegExComp(
<cRegExPattern> [, <lCaseSensitive>, <lMultiLine>]) ➜ pCompRegex
compiles a regular expression pattern.-
<cRegExPattern>is the regular to be compiled. -
<lCaseSensitive>specifies whether compiled reg-expression will be case-sensitive or not. (default.T.) -
<lMultiLine>specifies whether the compiled reg-expression will use the multi-line mode for matching. (default.F.)
The function returns either a
POINTERto regex-code, if<cRegExPattern>has been compiled successfully, orNILon failure (f.e. when no valid<cRegExPattern>was used). -
-
hb_IsRegEx(
<uExp>) ➜ lIsCompRegEx
returns.T.if<uExp>is a compiled regular expression, otherwise.F. -
hb_RegEx(
<hRegEx>|<cRegEx>, <cText> [ , <lCaseSensitive>, <lMultiLine> ]) ➜ aMatch
1st param is the matching pattern and can be a compiled (<hRegEx>) or a raw (<cRegEx>) regular expression.
2nd param<cText>is the string that is being scanned for matches.
returns an array of strings with the first full-match text and it’s corresponding sub-matches texts, if any.
Structure of the array that is returned:aMatch := { cFullMatch [, cSubMatch1, ..., cSubMatchN] }
If no match found, the returned array will be empty. -
hb_RegExAll(
<hRegEx>|<cRegEx>, <cText> [, <lCaseSensitive>, <lMultiline>, <nMaxMatches>, <nGetMatch>, <lOnlyMatch>]) ➜ aMatches
returns array of arrays with all the matches of the pattern in the string.<nMaxMatches>limits the number of matches to be found.
Structure of the array that is returned:aMatch := { {cFullMatch1 [, cSubMatch1, ..., cSubMatchN]}, {...} }
If no match found, returned array will be empty. (for details about optional params, refer to Juan's relevant page). -
hb_RegExLike(
<hRegEx>|<cRegEx>, <cText> [, <lCaseSensitive>] [, <lMultiLine>]) ➜ lMatches
returns .T. if a compiled (<hRegEx>) or a raw (<cRegEx>) regular expression matches the complete content of<cText>. -
hb_RegExHas(
<hRegEx>|<cRegEx>, <cText> [, <lCaseSensitive>] [, <lMultiLine>]) ➜ lMatches
returns .T. if a compiled (<hRegEx>) or a raw (<cRegEx>) regular expression is found in<cText>. -
hb_RegExSplit(
<hRegEx>|<cRegEx>, <cText> [, <lCaseSensitive>] [, <lMultiLine>] [, <nMaxMatches>]) ➜ aChunks
returns an array of strings where each element is the sub-string between matches. In other words, each match of the pattern in<cText>will act as a separator and the text not included in the match will be returned.
<nMaxMatches>are the maximum matches allowed as separators after wich there won’t be any more matches. If<nMaxMatches>is not specified, there is no limit for separators matching. -
hb_RegExAtX(
<hRegEx>|<cRegEx>, <cText> [, <lCaseSensitive>] [, <lMultiLine>]) ➜ aMatch
Returns an array with the first match and the corresponding sub-matches (if any) with the starting and ending position of each one (absolute positions in<cText>).
As an example usage of some regex functions, see below a piece of code, useful to check validity of a given IP address:
/*
* IP validity check,
* exploiting Harbour Regex functions.
*
* The regular expression used, is derived
* from here: https://www.regular-expressions.info/ip.html
*
* Pete D. 03/02/2018
*/
PROCEDURE Main( cpIP )
LOCAL cIP := hb_DefaultValue( cpIP, "192.168.1.1" )
LOCAL cRegex := "\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.)" + ;
"{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\z"
LOCAL pRegex := hb_RegExComp( cRegex )
? "Check IP validity"
? "Given IP address:", cIP
? "RegEx used :", cRegex
? "compiled RegEx :", pRegex, ValType( pRegex), hb_IsRegEx( pRegex )
?
? "hb_RegExLike()"
? cIP + " is" + Iif( hb_RegExLike( pRegEx, cIP ), "", " not") + " a valid IP. (compiled regex)"
? cIP + " is" + Iif( hb_RegExLike( cRegEx, cIP ), "", " not") + " a valid IP. (raw regex)"
?
? "hb_RegExHas()"
? cIP + " is" + Iif( hb_RegExHas( pRegEx, cIP ), "", " not") + " a valid IP. (compiled regex)"
? cIP + " is" + Iif( hb_RegExHas( cRegEx, cIP ), "", " not") + " a valid IP. (raw regex)"
?
RETURN🔙 Home
* Copyright © 2016–present Pete D.