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

    • youtuber
      StringRegExp Question
      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
      Need help with StringRegExp
      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
      Retrieve xtring from Google Search results
      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
       
    • Auto42
      RegExp help desired
      By Auto42
      Hello altogether,
      yes I already noticed the  StringRegExp AutoIt reference  and the very good german tutorial of SEuBo, but nevertheless I would be pleased to get a tip.
      I have strings like this:
      'repair car "do it your self" check' 
      or
      ' repair car toyota'.
      There could be none, one or more words in quotation marks. It could be also that no expression is set in quotation marks (-->" "). The words of the string vary. I would like to generate an array like [repair, car, do it your self, check] or [repair, car, toyota]. Expressions in quotation marks (" ") should not be splitted.
      Probably I could use string split and similar methods to combine the words in quotation marks afterwards. But that's not the way I'd like to do it.
      I already got this reg-exp to extract just the complete expressions in quotation marks (" ") to seperate these from the rest of the terms:
      Local $aExtract4 = StringRegExp($sTest4, '.*?"(.*?)".*?',3) Moreover I found this reg-exp to work similar to StringSplit with a " " as a delimiter. This would probably a solution if I didn't like to keep the terms in quotations marks together.... :
      Local $aExtract8 = StringRegExp($sTest4, '(.+?(?:\s|\z))',3) Nevertheless I failed to find a single reg-exp that is able to met both conditions. I am convincend that it is possible to design a single reg-exp that met both conditions! I already tried different scenarios with operators like or |, the conditional (?(?=   ) clause and the use of assertion (Lookahead / -behind) but without success.
      I would appreciate a hint from an "reg-exp expert" if it's able to solve my problem with a single reg-exp and / or to give me some hints how to do that.
      Many thanks in advance.
      Auto42
    • Italiano
      StringInStr - can this script be faster? (texts files included)
      By Italiano
      Hello,
      As always, sorry for my bad english.
      here is the code i have
      #include <File.au3> #include <String.au3> $file1 = "d:\doppioniautoit\international.txt" FileOpen($file1, 0) $file2 = "d:\doppioniautoit\standard.txt" FileOpen($file2, 0) For $i = 1 to _FileCountLines($file1) $line = FileReadLine($file1, $i) $aExtract = _StringBetween($line, "(", ")") ;MsgBox(0, $line, $aExtract[0]) $itime = TimerInit() For $x = 1 to _FileCountLines($file2) $line2 = FileReadLine($file2, $x) Local $iPosition = StringInStr($line2, $aExtract[0], 1) ;Local $iPosition = StringRegExp($line2,$aExtract[0], 0) if $iPosition <> 0 then ;MsgBox(0, "Trovato", $aExtract & " " & $line2) endif ConsoleWrite($line2 & @CRLF) Next ConsoleWrite(@TAB&'Str='&TimerDiff($itime)&' ms'&@lf) MsgBox(0, "TIME", @TAB&'Str='&TimerDiff($itime)&' ms'&@lf) Next FileClose($file1) So, what do i want to do? I try to explain with my poor english   Basically, i have 2 text files (see attachments below). They both contains movie titles with Director and Year  in this form
      Movie Title (Director, Year)
      "Standard.txt" contains, mostly, italian titles. "International.txt", as you can image, contains the internationals one. With the script i would like to search for the Director, Year of "international.txt" in the "standard.txt" file.
      For example... first row of "international.txt" is "¡Atraco! (Cortés, 2012)". The script takes just the "Cortés, 2012" and it searches for it in the standard. txt file.
      The simple code i wrote works...  I tried using StringInStr and using StringRegExp.. they both need about 2 minutes and 30 seconds (stringinstr is little faster) to process one row.
      I was wondering... is there any other method to make it faster using autoit? Any help would be much appreciated, thx!
       
       
      standard.txt
      international.txt