david1337

StringRegExp exact match

6 posts in this topic

Hi guys

I hope you can help me out with this one.

I have a text file "test.txt" which could contain something like this:

__________________________________________________________

fiw
eqw
sdg
xcv

__________________________________________________________

Each string has it's own line.

 

I use this script to find a string in the text file.

In this example, the string I am looking for is "iw".
The string "fiw" makes it believe that "iw" is found in the file. How can I avoid this?
I want it only to return EXACT results.
 

$string = "iw"

$File = FileOpen("test.txt", 0)
$ReadFile = FileRead($File)
FileClose($File)

If StringRegExp(StringLower($ReadFile), StringLower($string)) Then

MsgBox(0,"",$string & " is found in the file")

Exit

Else

MsgBox(0,"",$string & " is not found in the file")

Exit

EndIf

 

Share this post


Link to post
Share on other sites



david1337,

Use "beginning and end of string" anchors around the required string like this:

$string = "iw"

$ReadFile = "fiw" & @CRLF & "eqw" & @CRLF & "sdg" & @CRLF & "xcv"

If StringRegExp(StringLower($ReadFile), "^" & StringLower($string) & "$") Then
    MsgBox(0, "", $string & " is found in the file")
Else
    MsgBox(0, "", $string & " is not found in the file")
EndIf

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites

Melba,
Please don't forget the (?m) option  :)

1 person likes this

Share this post


Link to post
Share on other sites

Hi Melba

Thanks for your answer!
It seems that with your code, all strings gets the message "is not found in the file", even though I use "fiw" which is there?

 

Share this post


Link to post
Share on other sites

david1337,

mikell had the answer - you need to be in multiline mode:

$ReadFile = "fiw" & @CRLF & "eqw" & @CRLF & "sdg" & @CRLF & "xcv"

$string = "iw"
If StringRegExp(StringLower($ReadFile), "(?m)^" & StringLower($string) & "$") Then
    MsgBox(0, "", $string & " is found in the file")
Else
    MsgBox(0, "", $string & " is not found in the file")
EndIf

$string = "fiw"
If StringRegExp(StringLower($ReadFile), "(?m)^" & StringLower($string) & "$") Then
    MsgBox(0, "", $string & " is found in the file")
Else
    MsgBox(0, "", $string & " is not found in the file")
EndIf

Sorry about that - I had tested for the "wrong" case, but not for "right" one.

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

That worked perfectly.

Thanks for taking your time Melba and mikell :)

Edited by david1337

Share this post


Link to post
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

  • Similar Content

    • Subz
      By Subz
      Does anyone know how to split a string using multiple delimiters, returning both the values and delimiters withing the Array using StringRegExp?  For example:
      ;~ Split on " Not ", " And ", " Or " $sString = ' Not $a = 1 And $b = 2 Or $b = 3' $aArray = StringRegExp($sString,...) ;~ Returned Results $aArray[0] = '$a = 1' $aArray[1] = 'And' $aArray[2] = '$b = 2' $aArray[3] = 'Or' $aArray[4] = '$b = 3' At the moment I'm using
      Local $aArray1 = StringRegExp($sString, '(?i) Or | And | Not ', 3) Creating a new array using string split and then joining the two arrays together again
      Local $aArray1 = StringSplit(StringRegExpReplace($sString, '(?i) Or | And | Not ', '******'), '******', 3) Unfortunately regular expression isn't my forte.
    • Trong
      By Trong
      Hi,
      I need help string RegEx to get string from CREATE to GO
      #include <StringConstants.au3> ;~ Global $fileSQL1 = @ScriptDir & "\fileSQL1.sql" ;~ Global $fileSQL2 = @ScriptDir & "\fileSQL2.sql" Global $tmpSQLfile = @TempDir & "\tmpFile.sql" OnAutoItExitRegister("_OnExit") _SetTMPsql() If Not FileExists($tmpSQLfile) Then OnAutoItExitUnRegister("_OnExit") Exit MsgBox(48, "/!\", "File: " & $tmpSQLfile & @CRLF & " is not Exists!", 3) EndIf Global $ContentSQLfile = FileRead($tmpSQLfile) _Start() Func _Start() Local $aArray, $iOffset = 1, $stringRegExp = '(?i)CREATE(.*?)GO' While 1 $aArray = StringRegExp($ContentSQLfile, $stringRegExp, $STR_REGEXPARRAYMATCH, $iOffset) If @error Then MsgBox(48, "StringRegExp Error " & @error, "+> StringRegExp: " & $stringRegExp & @CRLF & @CRLF & "=> With STRING:" & @CRLF & @CRLF & $ContentSQLfile) ExitLoop EndIf $iOffset = @extended For $i = 0 To UBound($aArray) - 1 MsgBox(0, "RegExp Test with Option 1 - " & $i, $aArray[$i]) Next WEnd EndFunc ;==>_Start Func _SetTMPsql() Local $tmpSQLContent = "" $tmpSQLContent &= "USE [Master]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "CREATE DATABASE [Sales] ON PRIMARY " & @CRLF $tmpSQLContent &= "( NAME = N’Sales’, FILENAME = N’\FSASQLDBSales.mdf’ , " & @CRLF $tmpSQLContent &= " SIZE = 2GB , MAXSIZE = 8GB, FILEGROWTH = 1GB )" & @CRLF $tmpSQLContent &= "LOG ON " & @CRLF $tmpSQLContent &= "( NAME = N’Sales_log’, FILENAME = N’\FSASQLDBSales_log.ldf’ , " & @CRLF $tmpSQLContent &= " SIZE = 1GB , MAXSIZE = 2GB , FILEGROWTH = 10%)" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "USE [Sales]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- Table Product" & @CRLF $tmpSQLContent &= "CREATE TABLE [dbo].[Product]" & @CRLF $tmpSQLContent &= "(" & @CRLF $tmpSQLContent &= " [ProductId] [uniqueidentifier] DEFAULT NEWID() NOT NULL," & @CRLF $tmpSQLContent &= " [ProductName] [nchar](50) NULL," & @CRLF $tmpSQLContent &= " [ProductDescription] [nchar](3000) NULL," & @CRLF $tmpSQLContent &= " [ProductPrice] MONEY NULL" & @CRLF $tmpSQLContent &= ") ON [PRIMARY]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- Table Sales" & @CRLF $tmpSQLContent &= "CREATE TABLE [dbo].[Sales]" & @CRLF $tmpSQLContent &= "( " & @CRLF $tmpSQLContent &= " [SaleId] [uniqueidentifier] DEFAULT NEWID() NOT NULL," & @CRLF $tmpSQLContent &= " [SaleName] [nchar](50) NULL," & @CRLF $tmpSQLContent &= " [SaleInfo] [nchar](3000) NULL," & @CRLF $tmpSQLContent &= " [SaleMoney] MONEY NULL" & @CRLF $tmpSQLContent &= ") ON [PRIMARY]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "SET ANSI_NULLS ON" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "SET QUOTED_IDENTIFIER ON" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- The End" & @CRLF Local $hOpen = FileOpen($tmpSQLfile, 2 + 8 + 128) FileWrite($hOpen, $tmpSQLContent) Return FileClose($hOpen) EndFunc ;==>_SetTMPsql Func _OnExit() Exit FileDelete($tmpSQLfile) EndFunc ;==>_OnExit  
      mikell
       
    • youtuber
      By youtuber
      I do have a question
      <div class="col-sm-5 text-right"> <a href="/">IP Your adress: 113.228.141.182</a> </div>  
      Is it true?
      (?U)(\d+\d+\.\d+\.\d+\.\d+.\.)
    • helpus
      By helpus
      hello, i'm new to this function so i need some help doing this,
      btw, i need to clear up my folder of work by deleting some files
      example file inside folder :
      info000001 to info999999
      but i want to delete files name other than info(number)
      Func _delete() $dir = @ScriptDir & '\information' $cFiles = _FileListToArray($dir & '\', "*.xml", 1) If IsArray($cFiles) Then For $number To $cFiles[0] ;need to use StringRegExp here ;$stringRExp If $cFiles[$number] <> $stringRExp Then FileDelete($cFiles[$number] EndIf Next EndIf EndFunc  
    • hamohd70
      By hamohd70
      how can I use the StringRegEx function to extract certain lines of text from google search results in the following format:
      C: server port user password
      an example is this:
      C: starteam.myftp.org 18000 test1 text2
      regards