Jump to content

Help with StringRegExp


Recommended Posts

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

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

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 by youtuber
Link to comment
Share on other sites

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=ServerName
db.database.name=DatabaseName
db.instance.name=DatabaseInstanceName
db.port=1433


image.png.7e1f29f27a356180ff88da68bacfa7c1.png

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

  • 8 months later...

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

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 by mikell
Link to comment
Share on other sites

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

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