Jump to content

How to search only the second StringRegExp Or Second Random String?


Go to solution Solved by mikell,

Recommended Posts

Posted (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 question.png

database result.png

database.txt

Edited by HezzelQuartz
Posted

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!

Posted

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

Posted

@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

Posted
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

  • Solution
Posted (edited)

Thanks
BTW there was a typo in my previous expression :sweating:
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 by mikell
Posted

@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

Posted
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

 

Posted (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 by TheXman
Removed my example; OP seems intent on trying to process JSON with regular expressions, which is a "fool's errand".
  • 2 weeks later...
Posted

@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

 

================================================================================================================================

First Data.png

Result perfect.png

Posted (edited)
post deleted by pixelsearch (as my question was not related to this thread)
Edited by pixelsearch

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted (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 by mikell
errors
Posted

@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??

newdatabase.png

expected result.png

newresult.png

myresult.png

Posted (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 :sweating:
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 by mikell
Posted (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.

 

newest random database.png

database2.txt

Edited by HezzelQuartz

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...