Jump to content
florian2

Stringregexp : how to detect 2 letters in a line

Recommended Posts

florian2
Posted (edited)

I want the script to detect and erase every line who have the letters "a" and "e" in it.

ex.txt :

aeuio

euia

nike

nujik

I want the script to erase "aeuio" and "euia", so that it remain the following :

nike

nujik

thanks !

Edited by florian2

Share this post


Link to post
Share on other sites
Melba23

florian2,

So what code have you tried that has not worked?

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
florian2

it need to have both a and e so nike need to remain

Share this post


Link to post
Share on other sites
Chimp
Posted (edited)

tip: you could have a look to the StringInStr() and to the AND logical operator .....

... sorry, I see you want to use regexp ....

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
florian2

nah any tip is welcomed

Share this post


Link to post
Share on other sites
mikell
Posted (edited)

This removes lines which contain both "a" and "e"

$txt = "aeuio" & @crlf & _ 
    "euia" & @crlf & _ 
    "nike" & @crlf & _ 
    "nujik"

$txt = StringRegExpReplace($txt, '(?m)(?|^.*?a.*?e.*$|^.*?e.*?a.*$)\R?', "")

msgbox(0,"", $txt)

 

Edited by mikell

Share this post


Link to post
Share on other sites
Subz

FIrstly I suck at RegEx so take the following with a grain of salt.

#include <Array.au3>
Local $aSample[4] = ["aeuio", "euia", "nike", "nujik"]
_ArrayDisplay($aSample, "Sample Before")
For $i = UBound($aSample) - 1 To 0 Step - 1
    If StringRegExp($aSample[$i], "a.*e|e.*a") = 1 Then _ArrayDelete($aSample, $i)
Next
_ArrayDisplay($aSample, "Sample After")

 

  • Like 1

Share this post


Link to post
Share on other sites
mikell

Sorry. I didn't pay attention to the "both". Previous post edited

Share this post


Link to post
Share on other sites
florian2
Posted (edited)

thanks to both, but my challenge is to erase the lines from an external .txt, wich itself have many lines.

 

Like, i want to rewrite a new .txt file without the lines, or directly delete the concerned lines from it.

Edited by florian2

Share this post


Link to post
Share on other sites
Subz

Just use FileRead then FileOpen, FileWrite and then FileClose with the string or array results (I would go with mikell example) :) 

Share this post


Link to post
Share on other sites
mikell

...or _FileReadToArray and _FileWriteFromArray in a similar way (with Subz example)  :)

Share this post


Link to post
Share on other sites
florian2

ok thanks ill try that

Share this post


Link to post
Share on other sites
florian2

I found that, am i in the right direction ? 

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

;Assign the file path to a variable
Local $sFilePath = "C:\AutomationDevelopers\temp.txt"

;Open the file temp.txt in append mode. If the folder C:\AutomationDevelopers does not exist, it will be created.
Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)

;Display a message box in case of any errors.
If $hFileOpen = -1 Then
   MsgBox($MB_SYSTEMMODAL, "", "An error occurred when opening the file.")
EndIf

;Write a set of lines for demonstration
FileWriteLine($hFileOpen, "This is the first line")
FileWriteLine($hFileOpen, "This is the second line")
FileWriteLine($hFileOpen, "This is the third line")
FileWriteLine($hFileOpen, "This is the last line")

;Set the file position to beginning for reading the data from the beginning of the file.
FileSetPos($hFileOpen, 0, $FILE_BEGIN)

;Read the 2nd line of data from the file using the handle returned by FileOpen
Local $sFileRead = FileReadLine ($hFileOpen,2)

;Display the data.
MsgBox($MB_SYSTEMMODAL, "Automation Developers", "The second line is:" & @CRLF & $sFileRead)

;Read the last line of data from the file using the handle returned by FileOpen
Local $sFileRead = FileReadLine ($hFileOpen,-1)

;Display the data.
MsgBox($MB_SYSTEMMODAL, "Automation Developers", "The last line is:" & @CRLF & $sFileRead)

;Close the handle returned by FileOpen.
FileClose($hFileOpen)

 

Share this post


Link to post
Share on other sites
florian2
Posted (edited)

But there is millions of lines to check, so i dunno if fileopen/read can do the trick then

Edited by florian2

Share this post


Link to post
Share on other sites
Earthshine

reading one line at a time to an array, then process array? will that do it? that way, it will take many small swipes at the line. you could even break it down to word by word search, no?


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
mikell

Maximum string length in AutoIt is 2,147,483,647 . Have a try...

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

I was thinking going through the file, like this above... check it. there is example code in that thread I think you might find interesting. Happy programming.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
florian2
Posted (edited)

so far :

#include <file.au3>

$file = FileOpen("C:\AutomationDevelopers\temp.txt", 0)

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $line= StringRegExpReplace($line, '(?m)(?|^.*?a.*?e.*$|^.*?e.*?a.*$)\R?', "")
    MsgBox(0,'',$line)
WEnd
FileClose($file)

at least it show correcly line by line the right word, that encouraging. but how to actually delete the line in the file ?

 

 

 

 

Edited by florian2
  • Like 1

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

×