Jump to content

Need help in Search and match string


Recommended Posts

I have a txt file that I'm using to conduct a search for a match string "datasource.0.ch_id". Presently, this string is in line 91 of the txt file. The actual string in line 91 is datasource.0.ch_id=168486

I wish extract 168486 out, base on matching "datasource.0.ch_id" string. The problem is this string can be in line 90 to 110.

How do I get autoit to detect datasource.0.ch_id from line 90 to 110. There is only 1 instance of this string.

Have only started with basic code. I only how to perform 2 possible checks (line 91 or line 93) using if... then..else. As there are 20 possibility in 90 to 110 range, I think a more efficient code should be use. But I have no clue how to do that. Could anyone kindly help me with it? All these check will only return 1 value (as in the msgbox value). I have to figure out how to get other values in this similar sense (another 11 of them). Thanks.

$file =FileOpen ( "C:\customprogram\client\utility_context.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$chartid=FileReadLine  ($file,91); read chart id on line 91
$chartchk=StringRegExp ( $chartid, "datasource.0.ch_id" , 0 )
;MsgBox(0, "Chartchk1", $chartchk); 0 imply string not match , 1 imply string match
if $chartchk=0 then 
    $chartid=FileReadLine  ($file,93)
    $chartchk=StringRegExp ( $chartid, "datasource.0.ch_id" , 0 )
;ElseIf $chartchk=0 then
;       $chartid=FileReadLine  ($file,109)
    $Fchartid=StringMid ( $chartid, 20, 6 )
else 
$Fchartid=StringMid ( $chartid, 20, 6 ); base on line 93 input
endif

FileClose($file)

MsgBox(64,"Details",$Fchartid)
Link to comment
Share on other sites

Link to comment
Share on other sites

I would be interested to see if this works for you.

$file = FileOpen("C:\customprogram\client\utility_context.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

For $line = 90 To 110
    If StringInStr(FileReadLine($file, $line), "datasource.0.ch_id=") Then
        $Fchartid = StringRegExpReplace(FileReadLine($file, $line), "(\D.*|\d.*|)(datasource.0.ch_id)=(\d*)(\D.*|\d.*|)", "$3")
        $Online = $line
    EndIf
Next
FileClose($file)

MsgBox(64, "Details", " On line " & $Online & "  value = " & $Fchartid)

Edit Replaced RegExp "(\S?)datasource.0.ch_id=(\d+?)" with

"(\D*)(datasource.0.ch_id)=(\d*)(\D*)"

Allows for Text before and after search string.

Edit again: Replaced above with "(\D.*\d.*)(datasource.0.ch_id)=(\d*)(\D.*\d.*)"

If the search line is "line 19 asd saddatasource.0.ch_id=168486dfv tyu 66w"

$Fchartid = 168486

This is the last edit: Replaced above with "(\D.*|\d.*|)(datasource.0.ch_id)=(\d*)(\D.*|\d.*|)"

Now if line is as above or line is "datasource.0.ch_id=168486" when

$Fchartid = 168486

Edited by Malkey
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...