xHarbour Reference Documentation > Operator Reference |
Searches a character string for a matching regular expression.
<cString> HAS <cRegEx>
The HAS operator searches the character string <cString> for a substring based on the regular expression <cRegExp>. If the string contains a substring that matches the search pattern, the result of the operator is .T. (true). When no match is found, the result is .F. (false).
If the HAS operator is applied often in a search routine with the same regular expression, a literal regular expression <cRegEx> can be compiled with HB_RexExComp() to speed up operations.
See also: | $, AScan(), IN, LIKE, HB_RegEx(), HB_RegExComp(), HScan() |
Category: | Character operators , Comparison operators , Operators , Regular expressions , xHarbour extensions |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example uses two regular expressions to detect if // a character string contains a number or a Hex number. // Note that one regular expression is a literal string // while the second is compiled. PROCEDURE Main LOCAL aExpr := { "0xEA", "3.14", "aChar", DtoC( Date() ), "0x0d" } LOCAL cHex := HB_RegExComp( "0[xX][A-Fa-f0-9]+" ) LOCAL cString FOR EACH cString IN aExpr ? cString, "" IF cString HAS "[0-9].?[0-9]*" IF cString HAS cHex ?? "contains a Hex number" ELSE ?? "contains a number" ENDIF ELSEIF cString HAS cHex ?? "contains a Hex number" ELSE ?? "contains no number" ENDIF NEXT ** Output: // 0xEA contains a Hex number // 3.14 contains a number // aChar contains no number // 09/21/06 contains a number // 0x0d contains a Hex number RETURN
http://www.xHarbour.com