HezzelQuartz Posted August 9, 2023 Share Posted August 9, 2023 (edited) If I have a list of database attached below, How can I search so that I can get only 6 number as a result (1, 1, 2, 1, 1, 1) I use this below $database = FileRead(@ScriptDir & "\" & "database.txt") $database = StringRegExp($database, '"category": ' & '(.*)' & @CRLF & ' },' & @CRLF & ' ' & '(.*)', 3) _ArrayDisplay($database) The result attached below I Want it to only show the number (array 1, 3, 5, 7, 9 ,11) without comma behind Sorry for confusing explanation Thank you Edited: Sorry, One more question i also curious how can i search what line or row that number would be in txt file. database.txt Edited August 9, 2023 by HezzelQuartz Link to comment Share on other sites More sharing options...
rudi Posted August 9, 2023 Share Posted August 9, 2023 Hello, please post your file "database.txt" as text here. Where does this file come from? Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
mikell Posted August 9, 2023 Share Posted August 9, 2023 You must be more specific. Untested but this should work $database = StringRegExp($text, 'category":\h+([^"]+)"\s+},\s+(\d+)', 3) To get numbers only just remove the parenthesis of the first capturing group Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 9, 2023 Author Share Posted August 9, 2023 @mikell Do you mean like this? $database = StringRegExp($database, '"category": ' & @CRLF & ' },' & @CRLF & ' ' & '(.*)', 3) If so, It can't be displayed when I run it@rudi That is the only content inside the database.txt Link to comment Share on other sites More sharing options...
mikell Posted August 9, 2023 Share Posted August 9, 2023 35 minutes ago, HezzelQuartz said: That is the only content inside the database.txt You posted an image. Unusable for testing. Please post real txt 36 minutes ago, HezzelQuartz said: Do you mean like this? No. I mean the regular expression I provided, not less not more Link to comment Share on other sites More sharing options...
pixelsearch Posted August 9, 2023 Share Posted August 9, 2023 Something like this ? (?m)^\s*(\d+),?\s*$ HezzelQuartz 1 Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 9, 2023 Author Share Posted August 9, 2023 @mikell sorry, I just uploaded in my first post now Link to comment Share on other sites More sharing options...
Solution mikell Posted August 9, 2023 Solution Share Posted August 9, 2023 (edited) Thanks BTW there was a typo in my previous expression Here it is #Include <Array.au3> $text = FileRead(@ScriptDir & "\" & "database.txt") $database = StringRegExp($text, 'category":\h+"([^"]+)"\s+},\s+(\d+)', 3) _ArrayDisplay($database) To get numbers only, just remove the parenthesis of the first capturing group StringRegExp($text, 'category":\h+"[^"]+"\s+},\s+(\d+)', 3) You may also use the solution from pixelsearch StringRegExp($text, '(?m)\h*(\d+)', 3) But depending on the content of the text, it could be less reliable Edited August 9, 2023 by mikell HezzelQuartz 1 Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 9, 2023 Author Share Posted August 9, 2023 @mikell @pixelsearch Wow, Excellent. Amazing! Thank you Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 9, 2023 Author Share Posted August 9, 2023 @mikell Sorry for bothering you If you don't mind, could you please explain the meaning of each regex you use above? \h+"[^"]+"\s+},\s+(\d+) for example \h+ Matches any horizontal whitespace character , 1 or more, greedy. (\d+) Matches any decimal digit, Capturing group, 1 or more, greedy. I have read the help file, but still confused "[^"]+" ???? \s+},\s+ ??? Sorry, I Just learning Link to comment Share on other sites More sharing options...
mikell Posted August 9, 2023 Share Posted August 9, 2023 26 minutes ago, HezzelQuartz said: I have read the help file, but still confused "[^"]+" ???? \s+},\s+ ??? No problemo [^"] : a character class, matches anything which is not (because of the circumflex) a quote So "[^"]+" means : a quote, one or more non-quote chars, a quote \s : any white space char, horizontal or vertical (much better to use than working hard to include a @CRLF in the pattern) So \s+},\s+ means : one or more space chars, a closing brace, a comma, one or more space chars Link to comment Share on other sites More sharing options...
TheXman Posted August 9, 2023 Share Posted August 9, 2023 (edited) If you want to process the dataset as JSON, which it appears to be, then here's an example of one way that it can be done (using jq). If the data in the file is supposed to be a valid JSON dataset, then it is missing a "{" in the beginning and a "}" at the end. My example has added them in order to be able to process the data as JSON. Although the jq filter is pretty self-explanatory, I still added comments to make it as clear as possible how the supplied filter is capturing the data needed to create the result, which is used to populate the AutoIt array. <snip> Edited September 8, 2023 by TheXman Removed my example; OP seems intent on trying to process JSON with regular expressions, which is a "fool's errand". 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 Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 18, 2023 Author Share Posted August 18, 2023 @mikell and everyone What if I want to replace the number? I do it like this below #Include <Array.au3> $text = FileRead(@ScriptDir & "\" & "database.txt") $database = StringRegExpReplace($text, 'category":\h+"[^"]+"\s+},\s+(\d+)', 'category":\h+"[^"]+"\s+},\s+' & '50') FileWrite(@ScriptDir & "\" & "newdatabase.txt", $database) The result like this Link to comment Share on other sites More sharing options...
mikell Posted August 18, 2023 Share Posted August 18, 2023 2 hours ago, HezzelQuartz said: What if I want to replace the number? Like this : $text = FileRead(@ScriptDir & "\" & "database.txt") $s = StringRegExpReplace($text, 'category":\h+"[^"]+"\s+},\s+\K(\d+)', "50") Msgbox(0,"", $s) THe \K means "forget what was matched before" so you won't replace these parts pixelsearch and HezzelQuartz 1 1 Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 18, 2023 Author Share Posted August 18, 2023 @mikell Perfect!! Sorry, but I have more question How can I add any amount to the array I get before? For Example, First I have this code #Include <Array.au3> $text = FileRead(@ScriptDir & "\" & "database.txt") $database = StringRegExp($text, 'category":\h+"[^"]+"\s+},\s+(\d+)', 3) The Resullt Array below (1, 1, 2, 1, 1, 1) Then I want to use this code #Include <Array.au3> $text = FileRead(@ScriptDir & "\" & "database.txt") $database = StringRegExp($text, 'category":\h+"[^"]+"\s+},\s+(\d+)', 3) ; Here, I want to do array addition like $newdatabase[i] = $database[i] + 50 $newtext = StringRegExpReplace($text, 'category":\h+"[^"]+"\s+},\s+\K(\d+)', $newdatabase) FileWrite(@ScriptDir & "\" & "newdatabase.txt", $newtext) I Want to replace all array with new array that has been +50 So that Become like the picture below ================================================================================================================================ So, the question is how can I do something like these? For $i = 0 to Ubound($database) - 1 $newdatabase[$i] = $database[i] + 50 Next ================================================================================================================================ Link to comment Share on other sites More sharing options...
pixelsearch Posted August 18, 2023 Share Posted August 18, 2023 (edited) post deleted by pixelsearch (as my question was not related to this thread) Edited August 19, 2023 by pixelsearch Link to comment Share on other sites More sharing options...
mikell Posted August 18, 2023 Share Posted August 18, 2023 (edited) 6 hours ago, HezzelQuartz said: how can I do something like these? The multiple quotes everywhere make things much harder, reason why I suggest a solution in several steps... particularly if your next question is : how can I add 50 to the 1st category, 60 to the 2nd one, etc And I needed a trick to bypass the single quote in the text $text = FileRead(@ScriptDir & "\" & "database.txt") $text = StringReplace($text, "'", "§") $a = StringRegExp($text, 'category":\h+"[^"]+"\s+},\s+\d+', 3) Local $b[UBound($a)] For $i = 0 to UBound($a)-1 $b[$i] = Execute("'" & StringRegExpReplace($a[$i], "(\d)$", "' & $1+50 & '") & "'") $text = StringReplace($text, $a[$i], $b[$i]) Next $text = StringReplace($text, "§", "'") Msgbox(0,"", $text) Edit Using the "other" pattern to get the numbers, you can do it in one shot $text = FileRead(@ScriptDir & "\" & "database.txt") $text = StringReplace($text, "'", "''") $text = Execute("'" & StringRegExpReplace($text, "(?m)^(\s+)(\d)", "' & '$1' & $2+50 & '") & "'") Msgbox(0,"", $text) Edited August 18, 2023 by mikell errors Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 19, 2023 Author Share Posted August 19, 2023 @mikell and everyone I tried to change the database and using the first code from you $text = FileRead(@ScriptDir & "\" & "database.txt") $text = StringReplace($text, "'", "§") $a = StringRegExp($text, 'category":\h+"[^"]+"\s+},\s+\d+', 3) Local $b[UBound($a)] For $i = 0 to UBound($a)-1 $b[$i] = Execute("'" & StringRegExpReplace($a[$i], "(\d)$", "' & $1+50 & '") & "'") $text = StringReplace($text, $a[$i], $b[$i]) Next $text = StringReplace($text, "§", "'") Msgbox(0,"", $text) then the result show below but I expect the result to be as attached below (please see 4 pictures I attached below) ?? I also try edit my code like this #Include <Array.au3> $text = FileRead(@ScriptDir & "\" & "database.txt") $database = StringRegExp($text, 'category":\h+"[^"]+"\s+},\s+(\d+)', 3) ;_ArrayDisplay($database) Local $newdatabase[UBound($database)] For $i = 0 To UBound($database) - 1 $newdatabase[$i] = $database[$i] + 10 $newtext = StringRegExpReplace($text, 'category":\h+"[^"]+"\s+},\s+\K(\d+)', $newdatabase[$i]) Next FileWrite(@ScriptDir & "\" & "newdatabase.txt", $newtext) but all the number become 25 (I also attached the image below) I know, it's wrong, because it will use the last value of database to replace all my database. I just want to use it to describe here. How can I add any amount (e.g. 50) to my amount number?? Link to comment Share on other sites More sharing options...
mikell Posted August 19, 2023 Share Posted August 19, 2023 (edited) 12 hours ago, HezzelQuartz said: How can I add any amount (e.g. 50) to my amount number? Looks like you didn't clearly undrestand how my code works Using an array you can't do this in one go. You must separate the categories, then change the number in each of them, then replace in the main text this category by the "new category" with the new number Here it is, with explicit variable names and comments, and using a correct replacement character $text = FileRead(@ScriptDir & "\" & "database.txt") ;$text = StringReplace($text, "'", ChrW(0xFFFD) ) ; get the categories $categories = StringRegExp($text, 'category":\h+"[^"]+"\s+},\s+\d+', 3) ; visualize categories _ArrayDisplay($categories, "categories") ; get the numbers $numbers = StringRegExp($text, 'category":\h+"[^"]+"\s+},\s+(\d+)', 3) _ArrayDisplay($numbers, "numbers") ; loop through categories For $i = 0 to UBound($categories)-1 ; for each category, make the new number $new_n = $numbers[$i] +50 ; replace number by new number ;$new_cat = Execute("'" & StringRegExpReplace($categories[$i], "(\d+)$", "' & $new_n & '") & "'") $new_cat = StringRegExpReplace($categories[$i], '(\d+)$', $new_n) ; replace the category by the one modified $text = StringReplace($text, $categories[$i], $new_cat) Next ;$text = StringReplace($text, ChrW(0xFFFD), "'") Msgbox(0, "new text", $text) Edit I did no error checking, up to you how to do them And in the loop a simple SRER is enough as there is no operation in the 'replace' part Edited August 19, 2023 by mikell HezzelQuartz 1 Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 21, 2023 Author Share Posted August 21, 2023 (edited) @mikell and everyone Perfect, Thank you, I can finally do it now according to your explanation. Now, I have more question If I have a list of database attached below, How can I search so that I can get only 6 number as a result (13, 18, 25, 11, 16, 15) There is price, sometime is same, sometimes there is also discount but sometimes no discount appear in text. database2.txt Edited August 21, 2023 by HezzelQuartz Link to comment Share on other sites More sharing options...
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