cesinha87 Posted August 14, 2019 Share Posted August 14, 2019 Hi, I am trying to find specific info inside of a file and I am using StringReg and I am not able to figure out. The file looks like this db.server.name=ServerName db.database.name=DatabaseName db.instance.name= db.port=1433 db.user.name=Admin db.jdbc.driver=jtds db.param.ssl=request db.param.USENTLMV2=true db.pool.exhausted.action=grow Sometimes there is more or less information but it will always have db.server.name, db.database.name, db.port and I am trying to retrieve the information after the equals (=) character. I tried StringinString, StringTrimRight, and StringTrimLeft and worked on one machine but didn't work on another machine because like I said the txt file will have different amount of characters. This is why I switched to StringRegExp This is what I have so far $file = FileOpen($dbpropsfile & '\db.properties', 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf Global $readfile = $file $string = "db.server" If StringRegExp(StringLower($ReadFile), "(?m)^" & StringLower($string) & "$") Then MsgBox(0, "", $string & " is found in the file") Else MsgBox(0, "", $string & " is not found in the file") EndIf $string = "db.database" If StringRegExp(StringLower($ReadFile), "(?m)^" & StringLower($string) & "$") Then MsgBox(0, "", $string & " is found in the file") Else MsgBox(0, "", $string & " is not found in the file") EndIf Link to comment Share on other sites More sharing options...
Seminko Posted August 14, 2019 Share Posted August 14, 2019 Try this: $file = "db.server.name=ServerName" & @CRLF & _ "db.database.name=DatabaseName" & @CRLF & _ "db.instance.name=" & @CRLF & _ "db.port=1433" & @CRLF & _ "db.user.name=Admin" & @CRLF & _ "db.jdbc.driver=jtds" & @CRLF & _ "db.param.ssl=request" & @CRLF & _ "db.param.USENTLMV2=true" & @CRLF & _ "db.pool.exhausted.action=grow" MsgBox(1, "", $file) $string = "db\.server" If IsArray(StringRegExp($file, '^' & $string & '[^=]+=(.*)', 3)) Then MsgBox(0, "", $string & " is found in the file") Else MsgBox(0, "", $string & " is not found in the file") EndIf Link to comment Share on other sites More sharing options...
youtuber Posted August 14, 2019 Share Posted August 14, 2019 (edited) Maybe it will work $string = "db.server.name=ServerName" & @CRLF & _ "db.database.name=DatabaseName" & @CRLF & _ "db.instance.name=" & @CRLF & _ "db.port=1433" & @CRLF & _ "db.user.name=Admin" & @CRLF & _ "db.jdbc.driver=jtds" & @CRLF & _ "db.param.ssl=request" & @CRLF & _ "db.param.USENTLMV2=true" & @CRLF & _ "db.pool.exhausted.action=grow" $RegxSName = StringRegExp($string, "(?m)^db.server.name=(.*?)$", 3) If IsArray($RegxSName) Then For $a = 0 To UBound($RegxSName) - 1 $ServerName = $RegxSName[$a] Next EndIf $RegxDBName = StringRegExp($string, "(?m)^db.database.name=(.*?)$", 3) If IsArray($RegxDBName) Then For $a = 0 To UBound($RegxDBName) - 1 $DatabaseName = $RegxDBName[$a] Next EndIf $RegxDBPort = StringRegExp($string, "(?m)^db.port=(.*?)$", 3) If IsArray($RegxDBPort) Then For $a = 0 To UBound($RegxDBPort) - 1 $DPort = $RegxDBPort[$a] Next EndIf ConsoleWrite($ServerName & " " & $DatabaseName & " " & $DPort & @CRLF) Edited August 14, 2019 by youtuber Link to comment Share on other sites More sharing options...
cesinha87 Posted August 14, 2019 Author Share Posted August 14, 2019 It quiet didn't work as I expected I want to read the file and store in a variable, in this case, $read then use StringRegExp to extract the string after character = db.server.name=ServerNamedb.database.name=DatabaseNamedb.instance.name=DatabaseInstanceNamedb.port=1433 Link to comment Share on other sites More sharing options...
mikell Posted August 14, 2019 Share Posted August 14, 2019 Please try this #Include <Array.au3> $file = "db.server.name=ServerName" & @CRLF & _ "db.database.name=DatabaseName" & @CRLF & _ "db.instance.name=" & @CRLF & _ "db.port=1433" & @CRLF & _ "db.user.name=Admin" & @CRLF & _ "db.jdbc.driver=jtds" & @CRLF & _ "db.param.ssl=request" & @CRLF & _ "db.param.USENTLMV2=true" & @CRLF & _ "db.pool.exhausted.action=grow" $res = StringRegExp($file, 'db\.(?|server.name|database.name|instance.name|port)=(\N*)', 3) _ArrayDisplay($res) FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
cesinha87 Posted August 15, 2019 Author Share Posted August 15, 2019 Thank you all for the help. What Mikell recommended worked like a charm Link to comment Share on other sites More sharing options...
kamuline Posted April 17, 2020 Share Posted April 17, 2020 Hello I don't want to open a new topic, because my question is also for StringRegExp, and i can't find a"general StringRegExp questions" forum so i write here. Anyway i got an xml like txt files(attached), and i try to collect the all names and phone numbers from it, without success. There are my tries. Spoiler #include<file.au3> $hNumbers = FileOpen("numbers.txt",256) $bNumbers = FileRead($hNumbers) $array = StringRegExp($bNumbers, '(?s)(?:")(.*?)(?:" = ")(.*?)(?:")', 3) _ArrayDisplay($array) ;wrong $lines = _FileCountLines("numbers.txt") FileSetPos($hNumbers,0,$FILE_BEGIN) For $i = 1 to $lines $line = FileReadLine($hNumbers) $data = StringRegExp($line, '(?s)(?:")(.*?)(?:" = ")(.*?)(?:")', 3) _ArrayDisplay($data) ;wrong Next Is there any way to do it with StringRegExp? Thx numbers.txt Link to comment Share on other sites More sharing options...
mikell Posted April 17, 2020 Share Posted April 17, 2020 (edited) Maybe this ? #Include <Array.au3> $txt = StringReplace(FileRead("numbers.txt"), @crlf, ", ") $res = StringRegExp($txt, '([^"]+)" = "((?:Mobile|Home)[^"]+)', 3) Local $n = UBound($res), $k = 2, $res2D[Ceiling($n/$k)][$k] For $i = 0 To $n - 1 $res2D[Int($i / $k)][Mod($i, $k)] = $res[$i] Next _ArrayDisplay($res2D) Edit The trick is to use [^"] which means "any non-quote character"Mobile|Home is mentioned explicitely to avoid capture of "test" = "test" or so Edited April 17, 2020 by mikell kamuline 1 Link to comment Share on other sites More sharing options...
kamuline Posted April 17, 2020 Share Posted April 17, 2020 Thank you for the fast reply. I must say, for me it's hard to understand how is it working, but i try to learn from it. Big thx for this. Link to comment Share on other sites More sharing options...
Bert Posted April 17, 2020 Share Posted April 17, 2020 2 hours ago, kamuline said: Hello I don't want to open a new topic, because my question is also for StringRegExp, and i can't find a"general StringRegExp questions" forum so i write here. Anyway i got an xml like txt files(attached), and i try to collect the all names and phone numbers from it, without success. There are my tries. Reveal hidden contents #include<file.au3> $hNumbers = FileOpen("numbers.txt",256) $bNumbers = FileRead($hNumbers) $array = StringRegExp($bNumbers, '(?s)(?:")(.*?)(?:" = ")(.*?)(?:")', 3) _ArrayDisplay($array) ;wrong $lines = _FileCountLines("numbers.txt") FileSetPos($hNumbers,0,$FILE_BEGIN) For $i = 1 to $lines $line = FileReadLine($hNumbers) $data = StringRegExp($line, '(?s)(?:")(.*?)(?:" = ")(.*?)(?:")', 3) _ArrayDisplay($data) ;wrong Next Is there any way to do it with StringRegExp? Thx numbers.txt 481 B · 5 downloads This is the site I use for RegEx stuff: http://regexlib.com/Default.aspx kamuline 1 The Vollatran project My blog: http://www.vollysinterestingshit.com/ 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