Jump to content

find text and count one line to find text between !!


Recommended Posts

hello there and good morning ... 

i have this issue and i dont know how to write a code for solve it :D i have .txt ( source code ) 

Untitled.png

i want to search on value (c_user) --> unique then count one line down and show this number 732111212 in msg box

 

Link to comment
Share on other sites

You have to analyse the source code to determine how you can get the value.

For example, you can get the position of the string "<name>c_user</name>". From this position, you can search for the position of the string <value>.
Then add 7 to this position (the length of the string "<value>" and you will have the position of the "c_user" value.
Now next step, get the position of the string "</value>" from the last position, it will help you to determine the length of the value between <value> and </value>.

Please try by yourself and come back with your questions.

 

Link to comment
Share on other sites

okay this is my code i found the value  c_user  in my .txt and i count down one line down 

#include <File.au3>
#include <Array.au3>
#NoTrayIcon
#include <String.au3>
#include <StringConstants.au3>

Global $file = @DesktopDir & "\q5.txt", $search = "c_user"
Global $iLine = 0, $sLine = ''
Global $hFile = FileOpen($file)



While 1
    $iLine += 1
    $sLine = FileReadLine($hFile)

    If StringInStr($sLine, $search)  Then
        For $i = $iLine+1 To $iLine+1
            ConsoleWrite($i & ':' & FileReadLine($hFile, $i) & @CRLF)
        Next
        ExitLoop
    EndIf
WEnd
FileClose($hFile)

know i need to show that line without 3736:<value>732351212</value> i want to show only the number in msgbox i try to use StringRegExp but it's complicated i cant's understand it can u help me please 

 

Link to comment
Share on other sites

i think this is the easiest way to do it:

#include <String.au3>
$var =  'sometextline1'&@CRLF
$var &= 'sometextline2'&@CRLF
$var &= 'sometextline3'&@CRLF
$var &= '<name>c_user</name>'&@CRLF
$var &= '<value>732351212</value>'
$var &= '<secure>yes</secure>'
$aFound = _StringBetween($var,'<name>c_user', '</value>',$STR_ENDNOTSTART )
$sfound=''
If IsArray($aFound) Then $sfound=StringReplace($aFound[0],'</name>'&@CRLF&'<value>','')
MsgBox(0,'c_user value',$sfound)

 

Link to comment
Share on other sites

@hani-dev : Using your FileReadLine method, you can do it in this way :

#include <String.au3>

Global $file = @DesktopDir & "\q5.txt", $search = "c_user"
Global $iLine = 0, $sLine = ''
Global $iFound = 0
Global $hFile = FileOpen($file)


While 1
    $iLine += 1
    $sLine = FileReadLine($hFile)
    If @error Then ExitLoop

    If StringInStr($sLine, $search)  Then
        $iFound = 1
    ElseIf $iFound Then
        ConsoleWrite("Line found : " & $sLine & @CRLF)
        $aValue = _StringBetween( $sLine, "<value>", "</value>")
        $search = $aValue[0]
        MsgBox(0, "", "value of " & $search & " : " & $aValue[0])
        ExitLoop
    EndIf
WEnd
FileClose($hFile)

Note the use of _StringBetween, which is useful for this job.

 

Another way can be to load the whole file and extract the data.

A regex can be used, but you need to learn the syntax (no so easy) :

Global $file = @DesktopDir & "\q5.txt", $search = "c_user"
$sContent = FileRead($file)

$aValue = StringRegExp($sContent, "(?s)<name>" & $search & "<\/name>.+?<value>(\d+)", 1)
If @error Then
    MsgBox(16, "error", $search & " value not found")
Else
    MsgBox(0, "", "value of " & $search & " : " & $aValue[0])
EndIf

or using StringXXX functions :

$file = @DesktopDir & "\q5.txt"
$search = "c_user"
$sContent = FileRead($file)

$iNamePos = StringInStr($sContent, "<name>" & $search & "</name>")
$iValueStartPos = StringInStr($sContent, "<value>", 0, 1, $iNamePos) + 7
$iValueEndPos = StringInStr($sContent, "</value>", 0, 1, $iValueStartPos)
$value = StringMid($sContent, $iValueStartPos, $iValueEndPos - $iValueStartPos)
MsgBox(0, "", $value)

 

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...