Jump to content

Stringregexp : how to detect 2 letters in a line


Recommended Posts

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
Link to comment
Share on other sites

  • Moderators

florian2,

So what code have you tried that has not worked?

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

10 minutes ago, florian2 said:

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

Maybe the script will detect the "e" in "nike", not like you :) (I joke)

Link to comment
Share on other sites

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")

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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