_Func Posted June 13, 2023 Posted June 13, 2023 I have a text file and I read each line before doing a StringRegEx() Each line is similar to this text : 40397782_44_PC6654 97_1044_0040_05_02_001-CH1-Gate1-TOF.tif I Want extract "0040", I do : $a_Fit = StringRegExp($s_EndPath, "\d{2,}_(\d{4})_[^\.]+\.tif", 1) If @error = 0 Then ConsoleWrite("$a_Fit :" & $a_Fit[0] & @LF) EndIf If I put : $a_Fit = StringRegExp($s_EndPath, "\d{5,}_(\d{4})_[^\.]+\.tif", 1) The output is always 0040. Normally the returned value is none because there isn't 5 digits before the first "_".. After I must extract 1044, so I tested : $a_Fit = StringRegExp($s_EndPath, "_(\d{2,})_d{4}_[^\.]+\.tif", 1) If @error = 0 Then ConsoleWrite("$a_Fit :" & $a_Fit[0] & @LF) EndIf It doesn't work fine. I tested with $ operator at the end and it doesn't work anymore. The last value I want is "PC6654 97". As the first 2 didn't work, I didn't try to take out the 3rd one.
TheXman Posted June 13, 2023 Posted June 13, 2023 (edited) On 6/13/2023 at 5:41 PM, _Func said: I Want extract "0040"... After I must extract 1044 The last value I want is "PC6654 97" Expand Is your goal to learn regular expressions or is it just to be able to parse the values that you need from the file name? Below, you will find an example using StringRegExp() and StringSplit(). They both produce the exact same array of values. Also, as you can see, there's no need to do multiple function calls against the file name when you can get all of the values at once. expandcollapse popup#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d #include <Constants.au3> #include <Array.au3> Const $FILE_NAME = "40397782_44_PC6654 97_1044_0040_05_02_001-CH1-Gate1-TOF.tif" stringsplit_example() regex_example() Func stringsplit_example() Local $aParts = StringSplit($FILE_NAME, "_", $STR_NOCOUNT) ;Display parts ConsoleWrite("StringSplit Example" & @CRLF) ConsoleWrite($aParts[4] & @CRLF) ConsoleWrite($aParts[3] & @CRLF) ConsoleWrite($aParts[2] & @CRLF) ConsoleWrite(@CRLF) _ArrayDisplay($aParts, "StringSplit Example") EndFunc Func regex_example() Local $aParts = StringRegExp($FILE_NAME, "([^_]+)", $STR_REGEXPARRAYGLOBALMATCH) ;Display parts ConsoleWrite("Regex Example" & @CRLF) ConsoleWrite($aParts[4] & @CRLF) ConsoleWrite($aParts[3] & @CRLF) ConsoleWrite($aParts[2] & @CRLF) ConsoleWrite(@CRLF) _ArrayDisplay($aParts, "Regex Example") EndFunc Console output: StringSplit Example 0040 1044 PC6654 97 Regex Example 0040 1044 PC6654 97 Edited June 13, 2023 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
Nine Posted June 13, 2023 Posted June 13, 2023 This ? #include <Array.au3> Local $sText = "40397782_44_PC6654 97_1044_0040_05_02_001-CH1-Gate1-TOF.tif" Local $aData = StringRegExp($sText, "^.+?_.+?_(.+?)_(.+?)_(.+?)_", $STR_REGEXPARRAYMATCH) _ArrayDisplay($aData) “They did not know it was impossible, so they did it” ― Mark Twain Reveal hidden contents Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Shark007 Posted June 13, 2023 Posted June 13, 2023 (edited) I think he is after those specific positions. not the actual numbers in the test pattern. EDIT, I think Nine has it! Edited June 13, 2023 by Shark007
_Func Posted June 13, 2023 Author Posted June 13, 2023 Thanks to all. In attachment an extraction of the file I read. @Nine The Regex works fine with the line I put in the first post, but I saw some lines should not works or should not be processed. I'm sorry I didn't include a file in the first post. Thoses lines : 111_1044_4490_00_28_001-CH1-Gate1-AMP.tif 111 isn't a correct number of part. ESSAI_1044_4490_00_56_001-CH1-Gate2-TOF.tif ESSAI isn't a correct number of part. In fact I saw a number of part contain 1 or 2 letters and after many digits before the next underscore. For this lines : 40637383-11_HD112899 Y_1044_4490_00_18_001-CH1-Gate2-TOF.tif The first group is 1044, the 2nd => 4490 and the last => 00 The correct output should be 1° => HD112899 Y, 2nd => 1044 and the last 4490 Other example : 40744918-25_PC944867 8_1044_3035_00_05_002-CH1-Gate1-AMP.tif The first group is 1044, the 2nd => 3035 and the last => 00 The correct output should be 1° => PC944867 8, 2nd => 1044 and the last 3035 Extract.txtFetching info...
Nine Posted June 13, 2023 Posted June 13, 2023 Target moving ! Could you make a second column of this file indicating the expected result. We will stop guessing what you want... “They did not know it was impossible, so they did it” ― Mark Twain Reveal hidden contents Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
jugador Posted June 14, 2023 Posted June 14, 2023 #include <Array.au3> Local $sRegex = "(?m).+_(.+?)_(1044)_(.+?)_" Local $sString = "111_1044_4490_00_28_001-CH1-Gate1-AMP.tif" & @CRLF & _ "40637383-11_HD112899 Y_1044_4490_00_18_001-CH1-Gate2-TOF.tif" & @CRLF & _ "ESSAI_1044_4490_00_56_001-CH1-Gate2-TOF.tif" & @CRLF & _ "40397782_44_PC6654 97_1044_0040_05_02_001-CH1-Gate1-TOF.tif" Local $aArray = StringRegExp($sString, $sRegex, 3) _ArrayDisplay($aArray)
_Func Posted June 14, 2023 Author Posted June 14, 2023 I changed (1044) with (\d{4}) because it's not always 1044. It's always 4 digits but not always 1044 When I changed it doesn't work
_Func Posted June 14, 2023 Author Posted June 14, 2023 This regex works $a_Fit = StringRegExp($s_EndPath, "_([^_]+)_(\d{2,})_(\d{4})", 1) If @error = 0 Then ConsoleWrite($a_Fit[0] & ", " & $a_Fit[1] & ", " & $a_Fit[2] & @CRLF) EndIf
mikell Posted June 14, 2023 Posted June 14, 2023 On 6/13/2023 at 7:28 PM, _Func said: In fact I saw a number of part contain 1 or 2 letters and after many digits before the next underscore Expand ? #include <Array.au3> $s = FileRead("Extract.txt") $res = StringRegExp($s, "(?m)^.+_([A-Z].+?)_(\d+)_(\d+).+$", 3) ;_ArrayDisplay($res) Local $n = UBound($res), $k = 3 Local $res2D[Ceiling($n/$k)][$k] For $i = 0 To $n - 1 $res2D[Int($i / $k)][Mod($i, $k)] = $res[$i] Next _ArrayDisplay($res2D)
ioa747 Posted June 14, 2023 Posted June 14, 2023 StringSplit doesn't satisfies the criteria? ;~ For this lines : ;~ 40637383-11_HD112899 Y_1044_4490_00_18_001-CH1-Gate2-TOF.tif ;~ The first group is 1044, the 2nd => 4490 and the last => 00 ;~ The correct output should be 1° => HD112899 Y, 2nd => 1044 and the last 4490 Local $aGroup = _GetFileGroup("40637383-11_HD112899 Y_1044_4490_00_18_001-CH1-Gate2-TOF.tif") ConsoleWrite("$aGroup[0]=" & $aGroup[0] & @CRLF) ConsoleWrite("$aGroup[1]=" & $aGroup[1] & @CRLF) ConsoleWrite("$aGroup[2]=" & $aGroup[2] & @CRLF) ConsoleWrite("$aGroup[3]=" & $aGroup[3] & @CRLF) Func _GetFileGroup($sFileName) Local $aParts = StringSplit($sFileName, "_") Local $aResult[4] = [$sFileName, $aParts[2], $aParts[3], $aParts[4]] Return $aResult EndFunc ;==>_GetFileGroup I know that I know nothing
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now