Jump to content

StringRegExp doubt


Recommended Posts

Hi guys, I need your help. I have a text file and I want to capture the contents using regular expressions. The way the text is so.

/ / First line, all this after "/ /" are comments
text
text2 / / Comment
text3
/ / More comments
text with spaces
text with spaces1 / / more and more comments

and I need the following

text
text2
text3
text with spaces
text with spaces1

Any solution?

Sorry for my english :)

Link to comment
Share on other sites

  • Moderators

avechuche,

Welocme to the AutoIt forum. :)

The best I can do is this:

$sString = '/ / First line, all this after "/ /" are comments' & @CRLF & _
           'text' & @CRLF & _
           'text2 / / Comment' & @CRLF & _
           'text3' & @CRLF & _
           '/ / More comments' & @CRLF & _
           'text with spaces' & @CRLF & _
           'text with spaces1 / / more and more comments' & @CRLF & _
           'text4'

$sNewString = StringRegExpReplace($sString, "(?U)(.*)?(\/\s\/.*)(\v)", "$1$3")

MsgBox(0, "Result", $sNewString)

The blank lines should be easy enough to ignore! :whistle:

But if you wait a bit, a real guru will doubtless how us both how to do it properly. :D

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

Here is another possible solution.

$sString =  '/ / First line, all this after "/ /" are comments' & @CRLF & _
            'text' & @CRLF & _
            'text2 / / Comment' & @CRLF & _
            'text3' & @CRLF & _
            '/ / More comments' & @CRLF & _
            'text with spaces' & @CRLF & _
            'text with spaces1 / / more and more comments' & @CRLF & _
            'text4'

$sNewString = StringRegExpReplace($sString, "(?m)(^\/\h*\/.*\v*)|(\/\h*\/[^\v]*)", "")

MsgBox(0, "Result", $sNewString)
Link to comment
Share on other sites

Regular Expressions will always remain a mystery to me :unsure:

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You can try this using stringsplit

Local $file = FileOpen("test.txt", 0)
Local $outfile = "output.txt"

While 1
Local $line = FileReadLine($file)
If @error = -1 Then ExitLoop
$tmp = stringsplit($line, '/')
if $tmp[0] > 0 and $tmp[1] then filewriteline($outfile, stringstripws($tmp[1], 3))
WEnd

FileClose($file)

if you're set on using regex then try this:

Local $file = FileOpen("test.txt", 0)
Local $outfile = "output.txt"

While 1
Local $line = FileReadLine($file)
If @error = -1 Then ExitLoop
local $tmp = stringregexp($line, '^([^/]*)', 1)
if isarray($tmp) and $tmp[1] then filewriteline($outfile, stringstripws($tmp[1], 3))
WEnd

FileClose($file)
Edited by ChuckNorrious
Link to comment
Share on other sites

Regular Exp<b></b>ressi&#111;ns will always remain a mystery to me :unsure:

Do you want someone to decode it for you?

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

No, thanks. I will stick with string functions. I can do what I need to do with this functions.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

water,

I really would recommend trying to get a basic understanding of SREs. I agree that you can do most things with the String* functions, but there are times when only a SRE will do (and they are usually a lot faster as well). I do not consider myself very good at them, but being able to produce even a simple SRE (usually at the expense of a bit of time, hair and profanity) has been very useful on many occasions. ;)

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

I agree with Melba23 as well as water that sometimes string functions just serve a purpose without overcomplicating the situation with a SRE. But sometimes string functions just "can't cut the mustard."

#include <Constants.au3>

; With string functions.
Local $sVersion = '20.0.10.1000' ; Some version number.
Local $sStrippedVersion = '', $sChr = ''
For $i = 1 To StringLen($sVersion)
    $sChr = StringMid($sVersion, $i, 1)
    If StringIsInt($sChr) Then
        $sStrippedVersion &= $sChr
    EndIf
Next
MsgBox($MB_SYSTEMMODAL, '', $sStrippedVersion)

; With a regular expression.
Local $sVersion = '20.0.10.1000' ; Some version number.
Local $sStrippedVersion = StringRegExpReplace($sVersion, '\D', '')
MsgBox($MB_SYSTEMMODAL, '', $sStrippedVersion)

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

But sometimes string functions just "can't cut the mustard."

As a wise man once said "The thing about regexp's is knowing when to use them."

Who might have said that, M23?

kylomas

edit: ...Hmmmm, tongue in cheek does not translate well to this medium...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

I agree with Melba23 as well as water that sometimes string functions just serve a purpose without overcomplicating the situation with a SRE. But sometimes string functions just "can't cut the mustard."

And when SRE also fails, you just have to run multiple passes. I sometimes use SRE when it's not actually needed. :> It's good to see all these recent examples. I'm still only scratching the surface with SRE after two years. It's getting a lot easier though. :)

Edited by czardas
Link to comment
Share on other sites

OK, the next time I have to "cut mustard" I will give SRE a try ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

kylomas,

As a wise man once said "The thing about regexp's is knowing when to use them."

Who might have said that, M23?

It was actually GEOSoft when he was guiding me through my initial faltering steps with SREs. ;)

The problem I have is that I seem to have passed directly from the "crawling" stage to the "zimmer frame" stage without ever really making it to proper "walking" on the way! :D

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 all for the help! But now I come with two things

1) Comments with are "//", not "/ /" (no space between the "/")

2) Somehow I can directly put the results into an array?

I use the following function

Func _llenarArray($sFileDir)

$fileRead = FileRead($sFileDir)
If Not @error Then
$aArray = StringRegExpReplace($fileRead, "(?m)(^//.*\v*)|(//[^\v]*)", "")
$aArray = StringRegExp($aArray, "\V+", 3)
If Not @error Then Return $aArray
EndIf

EndFunc ;==>_llenarArray

3) Someone can explain me who used the ER? I understand the following

(?m), not exactly what function does this, I read in the help but I do not understand

(^//.*v*), I understand that all this text after "//", it deleted, what role does not understand that this "v*"

(//[^v]*) Same as above, how it works "v*"

thanks for everything

Edited by avechuche
Link to comment
Share on other sites

Here is an example with the regular expression patterns explained.

#include <Array.au3>

Local $sString, $aArr
$sString = '// First line, all this after "//" are comments' & @CRLF & _
'text' & @CRLF & _
'text2 // Comment' & @CRLF & _
'text3' & @CRLF & _
'// More comments' & @CRLF & _
'text with spaces' & @CRLF & _
'text with spaces1 // more and more comments' & @CRLF & _
'text4'
$aArr = _llenarArray($sString)
_ArrayDisplay($aArr)

#cs
(?m)^[^/\v]+ Explained
(?m)^ - Start matching at the beginning of each line.
[^/\v]+ - Match all characters, travelling travelling from left (the start of line) to right, that are
         not a "/" or any vertical white spaces (CR or LF).
         And, the "+" means those non-characters must exist at least once or many times for a match.


(?m)(^//.*\v*)|(//[^\v]*) Explained
(?m)     - Where "^" appears, and not inside square brackets, refers to the beginning of each line.
(^//.*\v*) - Capture the group of characters on a line that start with "//" followed by none or many
             any characters up to and including the vertical white spaces at the end of the line if it exists.
("*" meaning {0,} meaning the previous character or group can exist none or many times.)
|        - or (Match either the previous group or the following group.)
(//[^\v]*) - Capture the group of characters starting with "//" followed by none or many non-vertical white
             spaces.(That is: all characters that are not vertical white spaces like Carriage Returns (CR)
or Line Feeds (LF)).
             "(//[^\v]*)" is the same as "(//\V*)"
#ce
Func _llenarArray($fileRead) ; $sFileDir)
Local $aArray
;$fileRead = FileRead($sFileDir)
If Not @error Then
$aArray = StringRegExp($fileRead, "(?m)^[^/\v]+", 3)
MsgBox(0,"Text Message Box", StringRegExpReplace($fileRead, "(?m)(^//.*\v*)|(//[^\v]*)", ""), 4) ; For display purposes only.
If Not @error Then Return $aArray
EndIf
EndFunc ;==>_llenarArray

Edit:

avechuche

Might be best not to use my StringRegExp function to form an array because elements of the array will be formed when only one "/" is present.

Your example @ post#15 appears to work fine.

Edited by Malkey
Link to comment
Share on other sites

If you whish to preserve empty lines (typically used to make C++ style source easier to read by making blocks stand out), make that:

#include <Array.au3>

$sSource =  '// First line, all this after "//" are comments' & @CRLF & _
            'text' & @CRLF & _
            'text2 // Comment' & @CRLF & _
            'text3' & @CRLF & _
            '// More comments' & @CRLF & _
            '' & @CRLF & _
            'text with spaces' & @CRLF & _
            'text with spaces1 // more and more comments' & @CRLF & _
            'text4'

$sNoComment = StringRegExpReplace($sSource, "(?m)(^//.*\R)|(//[^\v]*)", "")
ConsoleWrite($sNoComment & @LF)
$aActualCode = StringRegExp($sNoComment, '(?m)(^.*)', 3)
_ArrayDisplay($aActualCode)

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

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