Search the Community

Showing results for tags 'regex'.

• Search By Tags

Type tags separated by commas.

Forums

• General
• Announcements and Site News
• AutoIt v3
• AutoIt Help and Support
• AutoIt Technical Discussion
• AutoIt Example Scripts
• Scripting and Development
• Developer General Discussion
• Language Specific Discussion
• Operating System Deployment
• Windows Client
• Windows Server
• Office

Categories

• AutoIt Team
• Beta
• MVP
• AutoIt
• Automation
• Databases and web connections
• Data compression
• Encryption and hash
• Games
• Hardware
• Information gathering
• Internet protocol suite
• Maths
• Media
• PDF
• Security
• Social Media and other Website API
• Windows
• Scripting and Development
• Operating System Deployment
• Windows Client
• Windows Server
• Office

Found 43 results

1. Regex... Repeating Block of Regex

I have an XML file and every time there are three lines in a row with only <null/> in them, i want to insert a fourth line with <null/>. Each line starts with 3 white spaces, followed by <null/> and ends with a white space followed by CR LF. The presence of the three lines as described is unique to the points where I want to insert a line in this document. I'm trying to figure out how to apply the repeating part of a regex {1,4} but apply it to this whole segment. So far I have the below which picks up an individual line ok: ^\s{3}<null/>\s\r\n I tried wrapping it all in braces () then adding {3} but I'm obviously getting something wrong. Attached is a section from the xml file with a block of nulls that should be matched if anyone would like to have a look. Help_From_Forum.xml
2. Searching Regex and define an array

I am trying to search in a web browser dates XX/XX/XXXX that are also links. I want to click them after and remove them from the array. This is all I have so far. Nothing shows up. What am I doing wrong? ControlFocus ("Customer Center", "", "") Local $aArray = StringRegExp('(..)/(..)/(....)', '(..)/(..)/(....)',$STR_REGEXPARRAYFULLMATCH) For $i = 0 To UBound($aArray) - 1 MsgBox($MB_SYSTEMMODAL, "RegExp Test with Option 2 - " &$i, $aArray[$i]) Next
3. StringRegExpReplace - replace 'COMMA' with ','

Hi All, I'd like to replace 'COMMA' with ',' for example: $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', ',') Now I've tried escaping the ',' in various ways unsuccessfully, such as: '[,]' "[,]" '\,' [,] seems to work in the pattern, I just can't figure out how to use it in the replace, and it seems everyone online is only interested in removing/replacing commas lol. I also tried creating and using a variable as the replacement but also didn't work: $myComma = ","$myString = "COMMA" StringRegExpReplace($myString, 'COMMA',$myComma) I'm sure it's super simple if someone could point me in the right direction - thanks.

Hi all, I still suck at regex as always and I need some help. According to the regex tester I normally use this should be working fine but it doesn't.... StringRegExp($sString, "\A[1-9]+[0-9]*(\-[1-9]+[0-9]*)?,*\Z") I basically want to match: all numbers EXCEPT 0, but including 10, 20, etc with each number separated by a comma and allowing a "-" separated range as a value For example: 1-5,7,10-12 I've spent a couple hours modifying it but I'm not sure where I've gone wrong. Any help would be appreciated! 5. StringRegExReplace in File. I have a file (see attached file) with a string all line and this problem on here is I want to separate all$00:, $03:,$10:, $20:,$25:, $30:,$40:, $45:,$110:, $115:,$120: and $T. It's mean that each$ with value start a new line ( a new paragraph). I tried with Regular Expression in notepad++ ex: Find ($00:,$01:, $03: and so on) with regex (\$)([0-9]+): and replace is \r\n\1\2 (I think \r\n is @CRLF (not sure :() ) Find $T with regex (\$T)(.*?)(\$T) and replace is \1\2\r\n\3 When I try these regex to replace in notepad on StringRegexReplace the results is incorrect . I have read some example simple about regex. Please advise me how to do that with some example on autoit . The result will be in attached photo. Thanks ahihi.txt 6. How to RegEx Global Variables Hi! I need a little bit help from some RegEx experts please: I would make my ISN AutoIt Studio faster when generating the scripttree. And what would be better to do this via regex? Problem is i am not really good at this regex stuff. So maybe someone could help me here. The challange is to get all Global Variables from a script via RegEx in a Array. Here is a example script with some tests: Global$Var1 = 1234 Local $Local_Var = 1234$Ignore_me_too = 1234 Global $Var2 = 1234,$var3 = 1242 Global $ahIcons[30],$ahLabels[30] Global Const $Var4 = iniread($inivar1,"jj","jj","") , $var5= iniread($inivar2,"jj","jj","") Global $Var_String = "was" Global$Array_Test[16] = [1,15,16,0,31,15,25,15,25,30,8,30,8,15,1,15] Global Enum $MARGIN_SCRIPT_NUMBER = 0,$MARGIN_SCRIPT_ICON, $MARGIN_SCRIPT_FOLD Global Const$Delim = '\', $Delim1 = '|' Global$hard1 = "a", _ $hard2 = "b", _$hard3 = "c" The returning array should look like this: $Var1$Var2 $var3$Var4 $var5$Var_String $Array_Test$MARGIN_SCRIPT_NUMBER $MARGIN_SCRIPT_ICON$MARGIN_SCRIPT_FOLD $Delim$Delim1 $hard1$hard2 $hard3 I already made some success with a expression i found in the SciTE Jump Tool: (\$\w+)(?:[\h$.=+*/^,)\-])? This nearly returns the perfect results. But it does not check if it´s a global variable (with the const and enum options) and also returns variables in commands (for example inivar1) I also found this regex: (?im:^(?=Global|Const|Enum|Static)(?:Global)?\h*(?:Const|Enum|Static)?(?:(?<=Enum)\h+Step\h+[+*-]\d+)?\h*)([^\r\n .\=]+) This returns also usefull results...but trying to understand this explodes my head Maybe someone can help me here? Thanks in advance! 7. Regex split quoted strings with escape quotes Can anyone tell me why this isn't working?.. #include <array.au3> regexp = StringRegExp("test 'a b c'", "'([^']|'')*'|\S+", 3) _ArrayDisplay(regexp) trying to split this "test 'a b c' 'some other '' test'' ...'" into: 0: test 1: 'a b c' 2: ... but it gives me: 0: test 1: c 8. Using regex to check the format of a string Hi guys, I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format: AA#####A Where the A's are any letter from A-Z and the #'s are any digit from 0-9. I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips? This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support. 9. [REGEX] Complex string to be validated Hello, i need to validate a string can be different things. I just need a True - False return value, no groups or things like that. It will be always one line at time to be processed by StringRegEx Valid: 13:52|String 02:52 XX|String 13:52~SUN, MON, TUE, WED, THU, FRI, SAT|String 02:52 XX~SUN, MON, FRI|String 22/04/2017 13:52|String 22/04/2017 02:52 YY|String Not Valid 22/04/2017 13:52~Dom|String I need to validate until and inclusively the | after that i don't care The XX and YY value are two sVariable from my script SUN, MON, TUE, WED, THU, FRI, SAT are fixed value, the can be mixed but always in the same order like SUN SUN, TUE, WED SUN, SAT The time can be 12 or 24 hours, the date is always in the same format DD/MM/YYYY. If there is a date can't be a day after that ( see not valid ) Well i think is all Sorry if i don't provide a working code, regex is too way complex. Thanks 10. FileFindFirstFile with internal Regex Hello. I need to list only the folders that has a name composed of numbers only. How do I do this? OBS: I just posted the relevant content to the folder. Global path = "G:\jobs\" For i = 1 To aArray[0] Local search = FileFindFirstFile(path & aArray[i] & "\[0-9]" ) I tried with "\*.*" and it returns me the expected result, but the other doesn't. 11. Need help with Regex to validate a UUID Hi all, it's been a while since I last used regular expressions and I find myself out of time to experiment with this particular issue, so I throw myself upon your mercy and expertise. I am looking to create a function that will say whether or not a supplied string is a valid UUID or not. Local sTestF = '4C4C4544-004A-4C10-8054-B7C04F46343' Local sTestT = '4C4C4544-004A-4C10-8054-B7C04F463432' ConsoleWrite('False = ' & _IsValidUUID(sTestF) & @CRLF) ConsoleWrite('True = ' & _IsValidUUID(sTestT) & @CRLF) Func _IsValidUUID(sUUID) ;[\p{XDigit}]{8}-[\p{XDigit}]{4}-[34][\p{XDigit}]{3}-[89ab][\p{XDigit}]{3}-[\p{XDigit}]{12} ; Test UUID = '4C4C4544-004A-4C10-8054-B7C04F463432' Local sRegExp = '([:xdigit:]){8}\-([:xdigit:]){4}\-([34])([:xdigit:]){3}\-([89ab])([:xdigit:]){3}\-([:xdigit:]){12}' ConsoleWrite(StringRegExp(sUUID, sRegExp) & @CRLF) Local Result = StringRegExp(sUUID, sRegExp) ConsoleWrite(Result & @CRLF) If @error Then ConsoleWrite('Error: [' & @error & ']' & @CRLF) Return 'False' Else ConsoleWrite('Error2: [' & @error & ']' & @CRLF) Return 'True' EndIf EndFunc In the line under the Function call, you'll see the regex I found to do this from a google search. That was my starting point, and I'm trying to get it to work in Au3 and failing miserably. sTestF is a known invalid String sTestT is a known valid String Everything I've tried so far has produced the same results for both. Any help you could provide me is greatly appreciated. Thanks for your time! 12. [Solved] StringRegExp to get string in SQL file Hi, I need help string RegEx to get string from CREATE to GO #include <StringConstants.au3> ;~ Global fileSQL1 = @ScriptDir & "\fileSQL1.sql" ;~ Global fileSQL2 = @ScriptDir & "\fileSQL2.sql" Global tmpSQLfile = @TempDir & "\tmpFile.sql" OnAutoItExitRegister("_OnExit") _SetTMPsql() If Not FileExists(tmpSQLfile) Then OnAutoItExitUnRegister("_OnExit") Exit MsgBox(48, "/!\", "File: " & tmpSQLfile & @CRLF & " is not Exists!", 3) EndIf Global ContentSQLfile = FileRead(tmpSQLfile) _Start() Func _Start() Local aArray, iOffset = 1, stringRegExp = '(?i)CREATE(.*?)GO' While 1 aArray = StringRegExp(ContentSQLfile, stringRegExp, STR_REGEXPARRAYMATCH, iOffset) If @error Then MsgBox(48, "StringRegExp Error " & @error, "+> StringRegExp: " & stringRegExp & @CRLF & @CRLF & "=> With STRING:" & @CRLF & @CRLF & ContentSQLfile) ExitLoop EndIf iOffset = @extended For i = 0 To UBound(aArray) - 1 MsgBox(0, "RegExp Test with Option 1 - " & i, aArray[i]) Next WEnd EndFunc ;==>_Start Func _SetTMPsql() Local tmpSQLContent = "" tmpSQLContent &= "USE [Master]" & @CRLF tmpSQLContent &= "GO" & @CRLF tmpSQLContent &= "" & @CRLF tmpSQLContent &= "CREATE DATABASE [Sales] ON PRIMARY " & @CRLF tmpSQLContent &= "( NAME = N’Sales’, FILENAME = N’\FSASQLDBSales.mdf’ , " & @CRLF tmpSQLContent &= " SIZE = 2GB , MAXSIZE = 8GB, FILEGROWTH = 1GB )" & @CRLF tmpSQLContent &= "LOG ON " & @CRLF tmpSQLContent &= "( NAME = N’Sales_log’, FILENAME = N’\FSASQLDBSales_log.ldf’ , " & @CRLF tmpSQLContent &= " SIZE = 1GB , MAXSIZE = 2GB , FILEGROWTH = 10%)" & @CRLF tmpSQLContent &= "GO" & @CRLF tmpSQLContent &= "" & @CRLF tmpSQLContent &= "USE [Sales]" & @CRLF tmpSQLContent &= "GO" & @CRLF tmpSQLContent &= "" & @CRLF tmpSQLContent &= "-- Table Product" & @CRLF tmpSQLContent &= "CREATE TABLE [dbo].[Product]" & @CRLF tmpSQLContent &= "(" & @CRLF tmpSQLContent &= " [ProductId] [uniqueidentifier] DEFAULT NEWID() NOT NULL," & @CRLF tmpSQLContent &= " [ProductName] [nchar](50) NULL," & @CRLF tmpSQLContent &= " [ProductDescription] [nchar](3000) NULL," & @CRLF tmpSQLContent &= " [ProductPrice] MONEY NULL" & @CRLF tmpSQLContent &= ") ON [PRIMARY]" & @CRLF tmpSQLContent &= "GO" & @CRLF tmpSQLContent &= "" & @CRLF tmpSQLContent &= "-- Table Sales" & @CRLF tmpSQLContent &= "CREATE TABLE [dbo].[Sales]" & @CRLF tmpSQLContent &= "( " & @CRLF tmpSQLContent &= " [SaleId] [uniqueidentifier] DEFAULT NEWID() NOT NULL," & @CRLF tmpSQLContent &= " [SaleName] [nchar](50) NULL," & @CRLF tmpSQLContent &= " [SaleInfo] [nchar](3000) NULL," & @CRLF tmpSQLContent &= " [SaleMoney] MONEY NULL" & @CRLF tmpSQLContent &= ") ON [PRIMARY]" & @CRLF tmpSQLContent &= "GO" & @CRLF tmpSQLContent &= "" & @CRLF tmpSQLContent &= "SET ANSI_NULLS ON" & @CRLF tmpSQLContent &= "GO" & @CRLF tmpSQLContent &= "SET QUOTED_IDENTIFIER ON" & @CRLF tmpSQLContent &= "GO" & @CRLF tmpSQLContent &= "" & @CRLF tmpSQLContent &= "-- The End" & @CRLF Local hOpen = FileOpen(tmpSQLfile, 2 + 8 + 128) FileWrite(hOpen, tmpSQLContent) Return FileClose(hOpen) EndFunc ;==>_SetTMPsql Func _OnExit() Exit FileDelete(tmpSQLfile) EndFunc ;==>_OnExit mikell 13. Title text match with spaces I am trying to rebuild an old single signon script that has been giving us a bit of trouble lately. I didn't write it and it was written in another script language. If the target program is already open it will have the current user's last name as part of the window text, which I am trying to compare against the user name to see if I should just WinActivate or Process Close and start the program over. I will be provided the username like "jones", or "smith" and the title check works fine for last names (with no spaces), but if the last name DOES contains spaces, say "Nom De Plume", then all I will be given as a username is "nomdeplume". Is there any way (via a regex maybe) that I can still check the text on the screen and somehow ? I have been reading about the Advanced (4) mode of WinTitleMatchMode, but it is over my head. Any help is appreciated. Here is a small reproducer script: Opt("WinTitleMatchMode", -1) Run("Notepad") WinWait("[CLASS:Notepad]", "", 10) Send ("Smith" & @CRLF & "Jones" & @CRLF & "Nom De Plume" & @CRLF) If WinExists("[CLASS:Notepad]", "jones") Then MsgBox(0,"FOUND","Found Jones") EndIf If WinExists("[CLASS:Notepad]", "nomdeplume") Then MsgBox(0,"FOUND","Found Nom De Plume") EndIf 14. Regex question Currently I have Switch StringLen(Serial) Case 0 Return "Please Enter a Serial #" Case 1 To 6 Return "UNKNOWN or INCORRECT" Case 7 Select Case StringLeft(Serial, 2) = "79" Valid = "Device 1" Case StringLeft(Serial, 3) = "350" Valid = "Device 2" Case StringRegExp(Serial, "72[HR](.*)") ; Match 72H and 72R Valid = "Device 3" Case Else Valid = "UNKNOWN or INCORRECT" EndSelect Case 8 Select Case StringRegExp(Serial, "\d[A-Z](.*)") ; Match a Digit (0-9), then a letter Valid = "Device 4 short format" Case Else Valid = "UNKNOWN or INCORRECT" EndSelect EndSwitch Which I'm using to match a number followed by a letter then any number of digits up to a string length of 8 characters. However I don't want it to match strings such as 1234A123 only 1D123456 Is there anyway to do this using a flag or am I limited to having to use StringLeft()? 15. Regex date Hello guys, i have a problem in this regex because its not groupping right. Look: (\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?) It checks for day or dayday, month or monthmonth, yearyear or yearyearyear+year. separated with or without space and the characters "\/.-" But, a problem! I tried to regex the date with or without the "separator + year" Eg: 05/05 or 05/05/05, but when i have 05/05/05, regex are separating 05/05 and /05 Iam using group "()" wrong? Need help here Thx adv! ~~EDIT: Script used: (from autoitscript.com/forum/topic/129697-split-string-by-regular-expression/#comment-901335) #include <Array.au3> Local sString = ClipGet() ; Extract the delimiters aDelim = StringRegExp(sString, '(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?)', 3) ; Split the string on the delimiters aParts = StringSplit(StringRegExpReplace(sString, '(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?)', "<SEPARATOR>"), "<SEPARATOR>", 3) ; Add the delimter to the start of the part For i = 0 To UBound(aParts) - 1 ;aParts[i + 1] = aDelim[i] & aParts[i + 1] Next ; Set the count aParts[0] = UBound(aParts) ; Display the result _ArrayDisplay(aParts) _ArrayDisplay(aDelim) Results in array: (the array parts in all string is ok; but the string delimitator himself is getting separated in every string that have year together) String used as test: Cheers. 16. Help with splitting a complex string Hello , I am trying to find the best way to spilt this string: :<nick>!<host mask> PRIVMSG <channel/nick> :<message> into an array like this: Global Enum SENT_BY, HOST_MASK, SENT_TO, MESSAGE aArray[SENT_BY] = "<nick>" aArray[HOST_MASK] = "<host mask>" aArray[SENT_TO] = "<channel/nick>" aArray[MESSAGE] = "<message>" (Stuff between <> is dynamic) I can do that with inefficient code but I think it is possible to do it more efficiently with RegEx Thanks in Advance! TD P.S Yeah, that is the raw format of a message received in IRC 17. Stuck in Regex world Hi ppl, im trying to do a script that reads an .au3 file, and retrieves all variables in it, then i work with that. This is what i got so far #include <Array.au3> File = FileOpenDialog('Au3 Var Searcher', @ScriptDir, 'Au3Files (*.au3)') FileOpen = FileOpen(File) FileRead = FileRead(FileOpen) FileClose(FileOpen) Search() Func Search() Local aArray = StringRegExp(FileRead, '[:alnum:][:blank:]', 2) ConsoleWrite(' - '& aArray[0] &' - '&@MSEC&@CRLF) _ArrayDisplay(aArray) EndFunc So we know it has to start with , and have [:alnum:] until [:blank:] or simply not [:alnum:] anymore. Or is there another way? Thanks in advance 18. I'm really stupid and can't figure out StringRegExp I have a String that I'm trying to get the text between of. The string is something like user/Dgameman1" class="author I'm trying to get everything between user/ and " class="author This is the code I currently have StringRegExp(HTML,'(\/user\/)(.+?)(\" class)', 3)But for whatever reason, this isn't returning the value and I'm not sure how to go about fixing the Regex Expression. Thank you guys so much 19. Input Validation Function So a few times I have wanted/needed some sort of input validation (aka input mask) we had an old udf somebody made that sort of worked but was hard to use and a bit limited. As I started to learn regex of late sort of by chance I realized it could be used as a pretty great input validation. But it was a lot of code if you wanted to do it for multiple fields. So wrapped it up into a little function and sharing. Far from a UDF so putting in the example section, and I am not sure if what I am doing is even "good code" but it works for me and hope it is usefull for somebody. Basic RegEx knowledge will help you make the most of this, but a few examples are included below in the code snippet. #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Form1 = GUICreate("Test GUI", 618, 276, 192, 124) Label1 = GUICtrlCreateLabel("Test GUI", 168, 24, 47, 17) Button1 = GUICtrlCreateButton("Toggle Validation", 290, 24, 110, 40) Input1 = GUICtrlCreateInput("Max 10 Characters", 40, 72, 553, 21) Input2 = GUICtrlCreateInput("Numb3rs 0nly", 40, 97, 553, 21) Edit1 = GUICtrlCreateEdit("Max 10 Chars Numbers Only + Auto Line Break", 40, 120, 553, 105) GUISetState(@SW_SHOW) iToggle = 1 While 1 Sleep(10) If Mod(iToggle, 2) = 0 Then ;Example 1 Limit to 10 Characters _GUIRegExValidate(Input1, "(.{10})(.)", "1") ;Example 2 Can only type numbers _GUIRegExValidate(Input2, "[^\d]", "") ;Example 3 Combind More Than One On Same Input limit to 10 characters, limit to numbers only, line break @ 10 characters to new line _GUIRegExValidate(Edit1, "(.{10})(.)", "1" & @CRLF) _GUIRegExValidate(Edit1, "[^\d\r\n]", "") EndIf nMsg = GUIGetMsg() Switch nMsg Case GUI_EVENT_CLOSE Exit Case Button1 iToggle = iToggle + 1 EndSwitch WEnd Func _GUIRegExValidate(sInputLabel, sRegExCapture, sRegExReplace) Step1Read = GUICtrlRead(sInputLabel) Step2Validate = StringRegExpReplace(Step1Read, sRegExCapture, sRegExReplace) If @Extended = 0 Then Return GUICtrlSetData(sInputLabel, Step2Validate) EndFunc Or per recommendation you can use it with the GuiRegisterMsg so that you can only process the RegEx when you make a chance instead of in a constant loop. #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Form1 = GUICreate("Test GUI", 618, 276, 192, 124) Label1 = GUICtrlCreateLabel("Test GUI", 168, 24, 47, 17) Button1 = GUICtrlCreateButton("Toggle Validation", 290, 24, 110, 40) Input1 = GUICtrlCreateInput("Max 10 Characters", 40, 72, 553, 21) Input2 = GUICtrlCreateInput("Numb3rs 0nly", 40, 97, 553, 21) Edit1 = GUICtrlCreateEdit("Max 10 Chars Numbers Only + Auto Line Break", 40, 120, 553, 105) GUISetState(@SW_SHOW) iToggle = 1 GUIRegisterMsg(WM_COMMAND, '_WM_COMMAND') While 1 Sleep(10) nMsg = GUIGetMsg() Switch nMsg Case GUI_EVENT_CLOSE Exit Case Button1 iToggle = iToggle + 1 EndSwitch WEnd Func _GUIRegExValidate(sInputLabel, sRegExCapture, sRegExReplace) Step1Read = GUICtrlRead(sInputLabel) Step2Validate = StringRegExpReplace(Step1Read, sRegExCapture, sRegExReplace) If @Extended = 0 Then Return GUICtrlSetData(sInputLabel, Step2Validate) EndFunc Func _WM_COMMAND(hWnd, Msg, wParam, lParam) If Mod(iToggle, 2) = 0 Then ;Example 1 Limit to 10 Characters _GUIRegExValidate(Input1, "(.{10})(.)", "1") ;Example 2 Can only type numbers _GUIRegExValidate(Input2, "[^\d]", "") ;Example 3 Combind More Than One On Same Input limit to 10 characters, limit to numbers only, line break @ 10 characters to new line _GUIRegExValidate(Edit1, "(.{10})(.)", "1" & @CRLF) _GUIRegExValidate(Edit1, "[^\d\r\n]", "") EndIf EndFunc 20. _ReplaceStringInFile() RegEx version? I was working on something last night and decided to use StringRegExpReplace() for a config file, I never noticed that you cant just "overwrite" the file with the update so easily it required a few more pieces of code to work properly. Is this the simplest way (what I used) and while I searched for it and did not find it do we have or will we have a RegEx equivalent for _ReplaceStringInFile()? sFile = FileRead(@ScriptDir & "\test.txt") hFile = FileOpen(@ScriptDir & "\test.txt", 2) sNewContent = StringRegExpReplace(sFile, "(test)", "new1") FileWrite(hFile, sNewContent) FileClose(hFile) 21. regex 'lazy' eye-opener needed I could use a lesson in laziness... I sometimes come across this problem where I think I need to use regex 'lazy' and the stuff I'm trying doesn't work, cos I need an eye-opener example for one of my typical problems... As an example a string of text where I *only* want the red spaces selected (or have a usable 1 and 2) after each piece of "quoted text" longer than 6 characters: "12345!" Txt "12345!" Txt "123456789!" Txt "12345!" Txt "12345!" Txt "12345678!" TxtThe problem with what I'm trying is that it seems greedy and the selection doesn't stop at the first quote character (going backwards)... I try '(?U)' and other stuff, but not seeing the light yet... This is *not* what I want. I want the "12345!" instances ignored... (BTW: don't use '\d', cos it could be anything inside the quotes) TIA 22. [RESOLVED] RegEx Resource Hello, I'm back to working on my RegEx skills and trying to come up with something to reformat file directory / file paths that are coming from other applications so that I can feed the resulting list into another script that I came up with. My problem is that I have been testing my RegEx by using https://regex101.com/ with PCRE(PHP). To date it has worked well for me, however, I have hit a stumbling block. In the example below the RegEx on line 158 when applied to pattern "C:\S\*" should pass through the ELSE on line 162 and then get trapped by line 175. Instead, it flows to 159 and then 188. I'm going cross-eyed and I can't spot the problem in the RegEx, If you have a moment and have experience with this kind of thing, would you please take a quick look and tell me what you think is wrong besides this being probably the ugliest way to go about a simple task. The task is simply end up with either a full file path or a directory without a trailing backslash. Thanks in advance, Casey EDIT - [RESOLVED] Replaced script block with corrections, though I am not at all happy with how sloppy it is and I am going to rewrite after learning quite a bit in the process. I just didn't want to leave a truly broken bit out here on the site. #include <Array.au3> #include <Constants.au3> #include <MsgBoxConstants.au3> ; Where we have a full path to a file including file extension Local aArray[1] = ["C:\S\File.txt"] _ArrayAdd(aArray, "C:\S\Some File.txt") _ArrayAdd(aArray, "C:\Windows\winsxs\FileMaps\program_files_business_objects_common_3.5_bin_d4f3c306b49748a7.cdf-ms") _ArrayAdd(aArray, "C:\S\0_Some File.cdf-ms") ; Where the training backslash is missing from a directory - This is good, DirGetSize format _ArrayAdd(aArray, "C:\S") ; Where the training backslash exists on a directory - This is bad, DirGetSize won't work _ArrayAdd(aArray, "C:\S\") ; Where the path ends in an * - This is bad, DirGetSize value less than C:\S _ArrayAdd(aArray, "C:\S\*") ; Where the path ends in an *.* - This is real bad, DirGetSize won't work and attrib is a laundry list _ArrayAdd(aArray, "C:\S\*.*") _ArrayAdd(aArray, "C:\Program Files (x86)\No Problem\*.*") _ArrayAdd(aArray, "C:\Program Files (x86)\No Problem\*.txt") _ArrayAdd(aArray, "C:\Program Files (x86)\No Problem\a.txt") _ArrayAdd(aArray, "C:\Users\A.Problem") _ArrayAdd(aArray, "C:\Users\A.Problem") ;======================================================================================================================================== ; A must as we are working with Winternal's Process Monitor Output and Handles.exe Output Local bArray = _ArrayUnique(aArray) _ArrayColInsert(bArray, 1) _ArrayColInsert(bArray, 2) _ArrayDisplay(bArray, "Array @ Beginning") ;======================================================================================================================================== Cnt = 0 For i = 0 To UBound(bArray) - 1 Cnt = Cnt + 1 Next Cnt = Cnt - 1 For i = 1 To Cnt Local LenRegEx = 0 Local LenArrayItem = 0 ;Test for something that might look like a file extension, greedy, but length of file extension and composition is unknown ;Example: C:\Windows\winsxs\FileMaps\program_files_business_objects_common_3.5_bin_d4f3c306b49748a7.cdf-ms Global cArray = StringRegExp(bArray[i][0], "^\w:\\[\.  \w \\ \-]+\\[\w \\ \.-]+", 2) If @error <> 0 Then ;_MsgBox("FIRST REGEX FAILED AND SET ERROR" & @CRLF & @CRLF & "LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) cArray = StringRegExp(bArray[i][0], "^\w:\\[\. \w\\\-]+\\[^\w \\ -]+", 2) If @error <> 0 Then Local LenRegEx = 0 Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox("SECOND REGEX FAILED AND SET ERROR" & @CRLF & @CRLF & "LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) bArray[i][2] = @ScriptLineNumber Else Local LenRegEx = StringLen(cArray[0]) Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox("LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) bArray[i][2] = @ScriptLineNumber EndIf Else Local LenRegEx = StringLen(cArray[0]) Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox("LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) bArray[i][2] = @ScriptLineNumber EndIf If LenRegEx == LenArrayItem Then ;_MsgBox("Some are FileGetSize and DirGetSize Compatible " & @CRLF & @CRLF & bArray[i][0] & @CRLF & @CRLF & cArray[0]) bArray[i][2] = @ScriptLineNumber IsThereAnAsterixOrSlash = StringMid(bArray[i][0], LenArrayItem, 1) ;_MsgBox("IsThereAnAsterixOrSlash = " & IsThereAnAsterixOrSlash) bArray[i][2] = @ScriptLineNumber Select Case IsThereAnAsterixOrSlash = "*" If StringMid(bArray[i][0], (LenArrayItem - 1), 2) = "\*" Then ;_MsgBox("A \* existed " & StringLen(bArray[i][0])) bArray[i][2] = @ScriptLineNumber bArray[i][1] = StringTrimRight(bArray[i][0], 2) ;_ArrayDisplay(bArray, @ScriptLineNumber) Else IsItAsterixDotAsterix = StringMid(bArray[i][0], LenArrayItem - 3, 4) ;_MsgBox("IsItAsterixDotAsterix: " & IsItAsterixDotAsterix) bArray[i][2] = @ScriptLineNumber If IsItAsterixDotAsterix = "\*.*" Then ;_MsgBox("\*.* " & @CRLF & @CRLF & IsItAsterixDotAsterix) ;_MsgBox("StringLen(bArray[i][0] " & @CRLF & @CRLF & StringLen(bArray[i][0]) & @CRLF & @CRLF & "StringLen(bArray[i][0] - 4 " & @CRLF & @CRLF & StringLen(bArray[i][0]) - 4) bArray[i][1] = StringTrimRight(bArray[i][0], StringLen(bArray[i][0]) - 4) ;_MsgBox("A * or \ existed " & bArray[i][0]) bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) ElseIf StringTrimRight(IsItAsterixDotAsterix, 2) = "\*" Then ;_MsgBox("\* " & @CRLF & @CRLF & IsItAsterixDotAsterix) ;_MsgBox("StringLen(bArray[i][0] " & @CRLF & @CRLF & StringLen(bArray[i][0]) & @CRLF & @CRLF & "StringLen(bArray[i][0] - 4 " & @CRLF & @CRLF & StringLen(bArray[i][0]) - 2) bArray[i][1] = StringTrimRight(bArray[i][0], StringLen(bArray[i][0]) - 2) ;_MsgBox("A * or \ existed " & bArray[i][0]) bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) Else ;_MsgBox("We shouldn't be able to see this so what's wrong?: " & @CRLF & @CRLF & bArray[i][0]) bArray[i][2] = @ScriptLineNumber EndIf EndIf Case IsThereAnAsterixOrSlash = "\" ;_MsgBox("A * or \ existed " & bArray[i][0]) bArray[i][2] = @ScriptLineNumber bArray[i][1] = StringTrimRight(bArray[i][0], StringLen(bArray[i][0]) - 1) ;_ArrayDisplay(bArray, @ScriptLineNumber) Case Else ;_MsgBox("We have a good full file path. " & @CRLF & @CRLF & bArray[i][0]) bArray[i][2] = @ScriptLineNumber bArray[i][1] = bArray[i][0] ;_ArrayDisplay(bArray, @ScriptLineNumber) EndSelect Else ;=========================================================================================================================== ;Check if no possible file extension by looking if returned regex length equals array element string length ;_MsgBox("LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) bArray[i][2] = @ScriptLineNumber ; Could be: C:\S\* dArray = StringRegExp(bArray[i][0], "^(.+\\\w+)", 2) If @error <> 0 Then ;_MsgBox("SECOND REGEX FAILED AND SET ERROR") bArray[i][2] = @ScriptLineNumber EndIf #cs Full String Match On: C:\S C:\S [return matches source, we have a winner and we already like this guy so we will do nothing to him] #ce LenRegEx = StringLen(dArray[0]) LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox("LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) bArray[i][2] = @ScriptLineNumber If LenRegEx == LenArrayItem Then ;_MsgBox("DIR NO SLASH " & dArray[0]) bArray[i][2] = @ScriptLineNumber bArray[i][1] = bArray[i][0] ;_ArrayDisplay(bArray, @ScriptLineNumber) Else eArray = StringRegExp(bArray[i][0], "^\w:\\[\.  \w \\ \-]+\\[^\w \\ \.-]+", 2) ;eArray = StringRegExp(bArray[i][0], "(?x) ^ \w : \\ [\. \w\\-]+ \\ [^\w\\ \.-]+", 2) [Same results as above] Thank you for the insight JCHD If @error <> 0 Then ;_MsgBox("THIRD REGEX FAILED AND SET ERROR") bArray[i][2] = @ScriptLineNumber eArray = StringRegExp(bArray[i][0], "^\w:\\[\.  \w \\ \-]+\\[^\w \\ \.-]?", 2) ; C:\S\ If @error <> 0 Then ;_MsgBox("FOURTH REGEX FAILED AND SET ERROR") bArray[i][2] = @ScriptLineNumber eArray = StringRegExp(bArray[i][0], "^\w:\\[\. \w\\\-]+\\[^\w \\ -]+", 2) ; C:\S\*.* -FAILING BC LINE 136 "^(.+\\\w+)" HAS PARTIAL MATCH If @error <> 0 Then ;_MsgBox("FIFTH REGEX FAILED AND SET ERROR") bArray[i][2] = @ScriptLineNumber Local LenRegEx = 0 Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox("INSANE ->" & bArray[i][0] & "<-") bArray[i][2] = @ScriptLineNumber Else Local LenRegEx = StringLen(eArray[0]) Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox(eArray[0] & @CRLF & @CRLF & LenRegEx) bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) EndIf Else Local LenRegEx = StringLen(eArray[0]) Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox(eArray[0] & @CRLF & @CRLF & LenRegEx) bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) EndIf Else Local LenRegEx = StringLen(eArray[0]) Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox(eArray[0] & @CRLF & @CRLF & LenRegEx) bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) If LenRegEx = LenArrayItem Then bArray[i][1] = bArray[i][0] bArray[i][2] = @ScriptLineNumber ;_MsgBox("LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) ;_ArrayDisplay(bArray, @ScriptLineNumber) EndIf EndIf fArray = StringRegExp(bArray[i][0], "^\w\:\\[\. \w\\\-]+\\[^\w \\ -]+", 2) If @error <> 0 Then bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) Else Local LenRegEx = StringLen(fArray[0]) Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox(fArray[0] & @CRLF & @CRLF & LenRegEx) bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) If LenRegEx = LenArrayItem Then bArray[i][1] = StringTrimRight(bArray[i][0], 4) ;bArray[i][1] = bArray[i][0] bArray[i][2] = @ScriptLineNumber ;_MsgBox("LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) LenRegEx = 0 ;_ArrayDisplay(bArray, @ScriptLineNumber) Else gArray = StringRegExp(bArray[i][0], "^\w\:\\[\. \w\\\-]+\\[^ \\ -]+", 2) If @error <> 0 Then bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) Else Local LenRegEx = StringLen(gArray[0]) Local LenArrayItem = StringLen(bArray[i][0]) ;_MsgBox("LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) If LenRegEx = LenArrayItem Then bArray[i][1] = StringTrimRight(bArray[i][0], 6) ;bArray[i][1] = bArray[i][0] bArray[i][2] = @ScriptLineNumber ;_MsgBox("LenRegEx = " & @CRLF & @CRLF & LenRegEx & @CRLF & @CRLF & "LenArrayItem = " & @CRLF & @CRLF & LenArrayItem) ;_ArrayDisplay(bArray, @ScriptLineNumber) EndIf EndIf EndIf EndIf IsThereAnAsterixOrSlash = StringMid(bArray[i][0], LenArrayItem, 1) ;_MsgBox(bArray[i][0] & @CRLF & @CRLF & "->" & IsThereAnAsterixOrSlash & "<-") bArray[i][2] = @ScriptLineNumber Select Case LenRegEx == LenArrayItem #cs Full String Match On: C:\S\ C:\S\* #ce If IsThereAnAsterixOrSlash = "*" Then bArray[i][1] = StringTrimRight(bArray[i][0], 2) ;_MsgBox("An * existed " & bArray[i][0]) bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) EndIf If IsThereAnAsterixOrSlash = "\" Then bArray[i][1] = StringTrimRight(bArray[i][0], 1) ;_MsgBox("A \ existed " & bArray[i][0]) bArray[i][2] = @ScriptLineNumber ;_ArrayDisplay(bArray, @ScriptLineNumber) Else ;_MsgBox("OUCH Who Knows") bArray[i][2] = @ScriptLineNumber EndIf Case Else ;_MsgBox("We shouldn't be able to see this so what's wrong?: " & @CRLF & @CRLF & bArray[i][0]) bArray[i][2] = @ScriptLineNumber EndSelect EndIf EndIf ;=========================================================================================================================== Next bArray[0][0] = "*** Source Format of Text ***" bArray[0][1] = "*** Reformatted Text ***" bArray[0][2] = "*** Script Line Exited ***" _ArrayDisplay(bArray, "Array @ End") ; #FUNCTION# ==================================================================================================================== ; CUSTOM MSGBOX - ADDS SCRIPT LINE NUMBER AS TITLE FOR DEBUGGING - ADD MSGBOXES TO SOURCE IF NEEDED ; EXAMPLE MSGBOX ;_MsgBox("The value for stringinput is not as expected == " & stringinput) Func _MsgBox(sText, sTitle = @ScriptLineNumber) MsgBox(0, "-" & sTitle & "-", sText) EndFunc ;==>_MsgBox ; =============================================================================================================================== 23. Regex 'headaches' + best browser for tester & more... 1) Was testing some code that would add a full stop after a sentence when it thinks it is proper. In my simplified example are just a small range of characters I want to exclude. However, although my code in the tester is what I want, my AutoIt version doesn't respect my exclusions and is adding full stops to the other characters too. #include <Array.au3> text = 'Possible end of a "sentence."' & @CRLF & @CRLF & 'Possible end of a sentence…' & @CRLF & @CRLF & 'Possible end of a sentence' & @CRLF & @CRLF & 'Possible end of a sentence!' & @CRLF & @CRLF & 'Last sentence.' MsgBox(0,"original text", text) _aFull_Stop_missing = StringRegExp( text, "(.[^.…!\""]\s{2,}.{3})", 3 ) If Not @error Then _ArrayDisplay(_aFull_Stop_missing) For i = 0 To UBound(_aFull_Stop_missing)-1 found = _aFull_Stop_missing[i] fix = StringLeft( found, 1) & "." & StringRight( found, StringLen(found) -1 ) text = StringReplace( text, found, fix, 1) Next EndIf MsgBox(0,"processed text", text) 2) I'm pretty sure I've seen (maybe older) info on how a StringRegExReplace can uppercase/lowercase a result. But I can't get to work any of what I saw... Neither of these are working here: text = "Http://site.com, Www.domain.org" result = StringRegExpReplace(text, "(?i)(https?|www)", StringLower("1") ) ToolTip(result) Sleep(2500) result = StringRegExpReplace(text, "(?i)(https?|www)", "\L1" ) ToolTip(result) Sleep(2500) 3) I've had the impression the https://regex101.com tester can give different results depending on the browser? If so, is there a preferred browser? 4) General question: I see both "If Not @error" and "If @error = 0" being used. "If Not @error" reads best to me. Is there a reason to not always use that variation? 5) If I am mostly processing text from the web or pdf, do I have to use a UTF setting in my regex everywhere/anywhere? So far I have not had that impression at all, but the first example made me start thinking if character encoding could be involved... (although adding UTF instructions in the regex didn't help) TIA!! 24. Regular expression How to use regular expressions in StringSplit and StringInStr 25. Regex - Drive letter - ellipses - two last folders from path Hallo Members, I'm looking for a good regex to get the drive letter and the last two folders from a file path, If the path is to long for the label width then show drive + ellipses and two last folders. Drive:\(ellipses)\folder\folder ex. D:\...\folder\folder and when the folder is in the root of the drive then show D:\Folder The test GUI #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.12.0 Author: Mecano Script Function: ELLIPSIS Long path: Drive:\...\Folder\Folder if root then Drive:\Folder #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here sFile = "F:\Just a folder\in a another folder\and another\This-dir\And-this-dir" EllipsisPath = StringRegExpReplace(sFile, '\w[a-zA-Z \$+\\', '') ; <- This needs another regex ;no ellipsis needed, for testing purposes only $sUSB = "K:\Just a folder"$PathforUSB = StringRegExpReplace($sUSB, '\w[a-zA-Z \\]+\\', '') #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3>$Form1 = GUICreate("Drive + Two last folders of path", 327, 236, 192, 124) $Button1 = GUICtrlCreateButton("Test label", 40, 168, 97, 33)$Label1 = GUICtrlCreateLabel("F:\...\This-dir\And-this-dir", 40, 12, 200, 40) ; <- Looks good but not dynamic GUICtrlSetColor($Label1, 32768)$Label2 = GUICtrlCreateLabel($sFile, 40, 40, 200, 40,$DT_END_ELLIPSIS) ; <- not the last two directorys GUICtrlSetColor($Label2, 16711680)$Label3 = GUICtrlCreateLabel("Var label1", 40, 72, 200, 40) GUICtrlSetColor($Label3, 16711680)$Label4 = GUICtrlCreateLabel("Var label", 40, 104, 200, 40) GUICtrlSetColor($Label4, 16711680) GUISetState(@SW_SHOW) While 1$nMsg = GUIGetMsg() Switch $nMsg Case$GUI_EVENT_CLOSE Exit Case $Button1 GUICtrlSetData($Label3, $EllipsisPath) ;$DT_END_ELLIPSIS <- works only on GUICtrlCreateLabel GUICtrlSetData($Label4,$PathforUSB) EndSwitch WEnd Thanks in advance
×
×
• Create New...