Jump to content

Can RegExp compare numbers inside of pattern ?


Iczer
 Share

Go to solution Solved by mikell,

Recommended Posts

  • Moderators

Iczer,

I think you will have to extract the group and then do the comparison. Can you let us have an example of $rawText and $pattern so we can experiment? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

How i can make pattern match only if Group #3 will capture number bigger than 1905? Less than 1812?

Whatever sensible set x belongs to,

(x > 1905) AND (x < 1812)

is only satisfied for elements of the empty set...

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

mikell good idea - just matching characters.

less than 1812
RE Pattern   0*d{1,3}   |   1[0-7]d{2}    |   18[0-1][0-1]
matches      000-0999  or 1000-1799   or 1800-1811

greater 1905
RE Pattern   [1-9]d{4,}       |   [2-9]d{3}     |   190[6-9]       |  19[1-9]d
matches      10000-infinity  or 2000-9999  or 1906-1909  or 1910-1999

#include <Array.au3>

Local $rawText = "8 12 345 wiz 1800 zip 1900 1811 1812 1813 1904 1905 1906 2001A 2000 bang 10000"

Local $sGroup1REPattern = "\b(0*\d{1,3}|1[0-7]\d{2}|18[0-1][0-1])\b" ; less than 1812
Local $aArray1 = StringRegExp($rawText, $sGroup1REPattern, 3)
_ArrayDisplay($aArray1, "less than 1812")

Local $sGroup2REPattern = "\b([1-9]\d{4,}|[2-9]\d{3}|190[6-9]|19[1-9]\d)\b" ; greater 1905
Local $aArray2 = StringRegExp($rawText, $sGroup2REPattern, 3)
_ArrayDisplay($aArray2, "greater 1905")

Local $sGroup3REPattern = "\b(0{0,}\d{1,3}|1[0-7]\d{2}|18[0-1][0-1]|[1-9]\d{4,}|[2-9]\d{3}|190[6-9]|19[1-9]\d)\b" ; less than 1812 or greater 1905
Local $aArray3 = StringRegExp($rawText, $sGroup3REPattern, 3)
_ArrayDisplay($aArray3, "(x<1812 or x>1905)")

Edit:-
To help create the regular expression patterns to find numbers greater than or less than specified numbers, I have created functions that will do the job.
Here is the example.
 

#include <Array.au3>

Local $rawText = "2 7 12 345 wiz 1800 zip 1900 1811 1812 1813 1904 1905 1906 2001A 2000 bang 10000 96 97 98"
Local $iNum1 = 1812
Local $iNum2 = 1905

; --------- Less than $iNum1 ------------------
Local $aArray1 = StringRegExp($rawText, _REPatternLessThan($iNum1), 3)
ConsoleWrite("LessThan " & $iNum1 & "  " & _REPatternLessThan($iNum1) & @LF)
_ArrayDisplay($aArray1, "less than " & $iNum1)

; --------- Greater than $iNum2 ------------------
Local $aArray2 = StringRegExp($rawText, _REPatternGreaterThan($iNum2), 3)
ConsoleWrite("GreaterThan " & $iNum2 & "  " & _REPatternGreaterThan($iNum2) & @LF)
_ArrayDisplay($aArray2, "greater than " & $iNum2)

; --------- Less than $iNum1 or greater than $iNum2 -----------
Local $aArray3 = StringRegExp($rawText, _REPatternLessAndGreaterThan($iNum1, $iNum2), 3)
ConsoleWrite(_REPatternLessAndGreaterThan($iNum1, $iNum2) & @LF) ;
_ArrayDisplay($aArray3, "(x<" & $iNum1 & " Or x>" & $iNum2 & ")")


; Creates Reg Exp Pattern which will match all numbers less than $iNumLessThan and all numbers greater than $iNumGreaterThan.
Func _REPatternLessAndGreaterThan($iNumLessThan, $iNumGreaterThan)
    Return StringTrimRight(_REPatternLessThan($iNumLessThan), 3) & "|" & _
            StringTrimLeft(_REPatternGreaterThan($iNumGreaterThan), 3)
EndFunc   ;==>_REPatternLessAndGreaterThan

; Creates Reg Exp Pattern which will match all numbers less than $iNum.
Func _REPatternLessThan($iNum)
    Local $aChars = StringRegExp($iNum, ".", 3), $iLen = UBound($aChars)
    Local $sRetPat = "\b("
    If $iLen <> 1 Then $sRetPat &= "0*\d{1," & $iLen - 1 & "}|"
    For $i = 0 To $iLen - 1
        If ($aChars[$i] <> 0) Then $sRetPat &= StringLeft($iNum, $i) & "[0-" & $aChars[$i] - 1 & "]\d{" & ($iLen - $i - 1) & "}|"
    Next
    $sRetPat = StringRegExpReplace($sRetPat, "(\\d\{0\}|\{1\}|\|$)", "") & ")\b" ; Erase "\d[0}" and "{1}" and trailing "|".
    Return StringRegExpReplace($sRetPat, "\[(\d)-\1\]", "\1") ; Replace "[n-n]" with "n".
EndFunc   ;==>_REPatternLessThan

; Creates Reg Exp Pattern which will match all numbers greater than $iNum.
Func _REPatternGreaterThan($iNum)
    Local $aChars = StringRegExp($iNum, ".", 3), $iLen = UBound($aChars)
    Local $sRetPat = "\b([1-9]\d{" & $iLen & ",}|"
    For $i = 0 To $iLen - 1
        If $aChars[$i] <> 9 Then $sRetPat &= StringLeft($iNum, $i) & "[" & $aChars[$i] + 1 & "-9]\d{" & ($iLen - $i - 1) & "}|"
    Next
    $sRetPat = StringRegExpReplace($sRetPat, "(\\d\{0\}|\{1\}|\|$)", "") & ")\b" ; Erase "\d[0}" and "{1}" and trailing "|".
    Return StringRegExpReplace($sRetPat, "\[(\d)-\1\]", "\1") ; Replace "[n-n]" with "n".
EndFunc   ;==>_REPatternGreaterThan
Edited by Malkey
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...