Jump to content

Find string in text file only if at start of line?


Recommended Posts

I can think of many inefficient ways to do this, but I wonder if there's a fast an efficient one. I

'm writing a script that reads in a text file and searches for a string like the following at the start of a line.

FONT = CONSOLAS

I don't want the search to find the same string in a line that reads something like:

rem FONT=CONSOLAS

I see that I can use FileReadLine to read the entire line (and maybe strip whitespace for consistency) and then use StringLeft to test the start of the line for REM or FONT, but is there a way to search ONLY for a string that is at the start of a line?

I think the answer may be obvious, but I can't figure out what it might be.

 

 

 

 

Link to comment
Share on other sites

Try this:

Local $sText = FileRead("myfile.txt")
Local $Found = StringRegExp($sText, "(?im)^\s*FONT\s*=\s*CONSOLAS\b")
MsgBox(0, "Result", "Search string was " & ($Found ? "" : "not ") & "found")

 

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

I'm not sure if this question belongs in a new thread, but it seems relevant enough to this one.

I've been spending the past hour trying to figure out how to get the line number of the text string (the line number after the newline character tested at the start of the regex). I want to read the line with the found string and copy it into another file, and can't find a method. Is it possible to combine the test for the string and the test for the line number in a single function?

Link to comment
Share on other sites

  • Moderators

emendelson,

Just ask the RegEx to extract the whole line:

#include <Array.au3>

$sText = "Not a line we want to read" & @CRLF & _
         "FONT = CONSOLAS - Remainder of line we do want" & @CRLF & _
         "rem FONT = CONSOLAS - But not this one"

Local $aFound = StringRegExp($sText, "(?im)^\s*FONT\s*=\s*CONSOLAS.*$", 3)

_ArrayDisplay($aFound, "", Default, 8)

All I did was to extend the capturing group to the end if the line and ask for matches to be returned rather than a simple True/False.

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

Thank you - but I think I must be too much of a newbie to understand how to use this. The _ArrayDisplay keeps telling me that the row number is 0, no matter how many lines I add to the string. Am I missing something really obvious? (That's very likely, unfortunately.)

Link to comment
Share on other sites

  • Moderators

emendelson,

The RegEx pulls the whole line ready for copying to the new file, so you have no need to know the actual line number. That is what you said you wanted:

Quote

 I want to read the line with the found string and copy it into another file

The "0" refers to the first element of the returned array from the RegEx - nothing at all to do with the line number within the original file. So all you need to do is use $aFound[0] as the data to write to the new file.

Clearer now?

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

Ah! I get it! You're right - that's all I needed, and I was too fixated on getting the line number to see that what you gave me was exactly what I was asking for...

Those three dots in the preceding line represent the three times I slapped my forehead after reading this.

Thank you again!

Link to comment
Share on other sites

  • Moderators

emendelson,

Glad we got it straightened out. And do not hit yourself too much - RegExes are really difficult to understand (I consider them the most difficult thing I have ever tried to learn in programming) and I stand in awe of the RegEx gurus around here.

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

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