Jump to content

need help with stringregexp involving multiple quoted words


adam
 Share

Recommended Posts

Looking to use stringregexp to see if: 

"status":"Offline"

is in file status.txt.

Note I need all 4 quotation marks to be present as well.

$string = """status":"Offline"""

FileOpen("status.txt")
$status = FileRead("status.txt")

If StringRegExp($status, $string) Then
    MsgBox(0, "", "true")
EndIf

It's returning true on the "status" part alone. I could have "status":"garbage" in there and will return true.

 

I found a work around by making another text file called string.txt and putting "status":"Offline" into it. I then changed as follows and it worked.

FileOpen("string.txt")
$string = FileRead("string.txt")

FileOpen("status.txt")
$status = FileRead("status.txt")

If StringRegExp($status, $string) Then
   MsgBox(0, "", "true")
EndIf

I'd appreciate help with setting $string to "status":"Offline" in the script itself. I am pretty sure I need to escape the quotation marks around "Offline"?

Edited by adam
Link to comment
Share on other sites

Why don't you use:  '"status":"Offline"'

That's your target string wrapped in single quotes.  Unless using a regular expression is a requirement, that should work in a simple StringInStr also.

Look at the "Strings" section under the data types topic for an explanation:  https://www.autoitscript.com/autoit3/docs/intro/lang_datatypes.htm

Edited by TheXman
Link to comment
Share on other sites

@adam : Here a quick example for @TheXman 's suggestion :

Local $sSearch = '"status":"Offline"'
FileOpen("status.txt")
Local $sRead = FileRead("status.txt")
ConsoleWrite($sRead & @CRLF)
If StringRegExp($sRead, '(?i)' & '\Q' & $sSearch & '\E') Then
    ConsoleWrite("+ Found     : " & $sSearch & @CRLF)
Else
    ConsoleWrite("! Not Found : " & $sSearch & @CRLF)
EndIf

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Thank you for the help.

I had initially tried ""status":"Offline"" but autoit wasn't happy with it.

I'll do some reading in that section because to me '"status":"Offline"' should be no different than ""status":"Offline""

Edited by adam
Link to comment
Share on other sites

8 minutes ago, adam said:

I need to do some reading because shouldn't '"status":"Offline"' be no different than ""status":"Offline""?

That would actually be:  """status"":""Offline"""

If you were going to use double quotes to enclose the string, then every double quote within the string would need to be escaped ("").

 

$string = """status"":""Offline"""
ConsoleWrite($string & @CRLF)

Console:

"status":"Offline"

 

Edited by TheXman
Link to comment
Share on other sites

Link to comment
Share on other sites

You can probably even omit " at the beginning and end of the string, and just search for status":"Offline

Local $sSearch = 'status":"Offline'

or  :lol::

Local $sSearch = 'status' & Chr(34) & ':' & Chr(34) & 'Offline'

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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