Example #1
0
def _loadRegExpr(parentContext, xmlElement, attributeToFormatMap,
                 formatConverterFunction):
    def _processCraracterCodes(text):
        """QRegExp use \0ddd notation for character codes, where d in octal digit
        i.e. \0377 is character with code 255 in the unicode table
        Convert such notation to unicode text
        """
        text = unicode(text)

        def replFunc(matchObj):
            matchText = matchObj.group(0)
            charCode = eval(matchText[1:])
            return chr(charCode).decode('latin1')

        return re.sub(r"\\0\d\d\d", replFunc, text)

    insensitive = _parseBoolAttribute(
        xmlElement.attrib.get('insensitive', 'false'))
    string = _safeGetRequiredAttribute(xmlElement, 'String', None)

    if string is not None:
        string = _processCraracterCodes(string)

        wordStart = string.strip('(').startswith('\\b')
        lineStart = string.strip('(').startswith('^')
    else:
        wordStart = False
        lineStart = False

    abstractRuleParams = _loadAbstractRuleParams(parentContext, xmlElement,
                                                 attributeToFormatMap,
                                                 formatConverterFunction)
    return _parserModule.RegExpr(abstractRuleParams, string, insensitive,
                                 wordStart, lineStart)
Example #2
0
def _loadRegExpr(parentContext, xmlElement, attributeToFormatMap,
                 formatConverterFunction):
    def _processCraracterCodes(text):
        """QRegExp use \0ddd notation for character codes, where d in octal digit
        i.e. \0377 is character with code 255 in the unicode table
        Convert such notation to unicode text
        """
        text = str(text)

        def replFunc(matchObj):
            matchText = matchObj.group(0)
            charCode = eval('0o' + matchText[2:])
            return chr(charCode)

        return re.sub(r"\\0\d\d\d", replFunc, text)

    insensitive = _parseBoolAttribute(
        xmlElement.attrib.get('insensitive', 'false'))
    minimal = _parseBoolAttribute(xmlElement.attrib.get('minimal', 'false'))
    string = _safeGetRequiredAttribute(xmlElement, 'String', None)

    if string is not None:
        string = _processCraracterCodes(string)

        strippedString = string.strip('(')

        wordStart = strippedString.startswith('\\b')
        lineStart = strippedString.startswith('^')
        if len(strippedString) > 1 and strippedString[
                1] == '|':  # ^|blabla   This condition is not ideal but will cover majority of cases
            wordStart = False
            lineStart = False
    else:
        wordStart = False
        lineStart = False

    abstractRuleParams = _loadAbstractRuleParams(parentContext, xmlElement,
                                                 attributeToFormatMap,
                                                 formatConverterFunction)
    return _parserModule.RegExpr(abstractRuleParams, string, insensitive,
                                 minimal, wordStart, lineStart)