xHarbour Reference Documentation > Operator Reference xHarbour Developers Network  

HAS

Searches a character string for a matching regular expression.

Syntax

<cString> HAS <cRegEx>

Arguments

<cString>
This is the character string being searched.
<cRegEx>
This is a character string holding the search pattern as a literal regular expression. Alternatively, the return value of HB_RegExComp() can be used.

Description

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.

Info

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

Example

// 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

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe