RegEx to Match Expression With Quotation Marks

Get help with using AutoHotkey and its commands and hotkeys
adamas
Posts: 6
Joined: 14 Nov 2017, 16:22

RegEx to Match Expression With Quotation Marks

20 Nov 2017, 17:06

Guys, I'm struggling with RegEx a bit (yet again). I'm trying to put together a regex to apply to expression like this

Code: [Select all] [Download] GeSHi © Codebox Plus

"videoId": "KhmrdFsY6Ls"
and match (so RegExMatch) whatever's in-between the "videoId": " part and the last ". So in the example above I need the regex to match the KhmrdFsY6Ls portion. I think I'm struggling here especially, due to the fact that there's a bunch of quotation marks in the string, so no matter how I try AutoHotkey gives me an error.

Here's the string I have so far that totally does not work:

Code: [Select all] [Download] GeSHi © Codebox Plus

RegExMatch(VideoLengthRaw, "O)"videoId": "(\d+)"", RegExResultHoldover), VideoLength := RegExResultHoldover[1]


Any ideas? :headwall:
User
Posts: 112
Joined: 26 Jun 2017, 08:12

Re: RegEx to Match Expression With Quotation Marks

20 Nov 2017, 17:14

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

text = "videoId": "KhmrdFsY6Ls"

RegExMatch(text, """.*?"".*?""(.*?)""", Matched)

msgbox, % Matched1
adamas
Posts: 6
Joined: 14 Nov 2017, 16:22

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 03:35

User wrote:

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

text = "videoId": "KhmrdFsY6Ls"

RegExMatch(text, """.*?"".*?""(.*?)""", Matched)

msgbox, % Matched1

Hey! Is there any way to make it narrow and specific to the string? The issue is that there are a ton of of information in the text where I'm trying to regex it that have the same pattern as the string I need. Example:
"nextPageToken": "CAEQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 245,
"resultsPerPage": 1


So I specifically need to match the text between "videoId": " and the last ", because otherwise it catches any other string but the one I need (since many similar come before the one I'm looking to match).
Odlanir
Posts: 47
Joined: 20 Oct 2016, 08:20

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 04:30

Some like this ?

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

adamas
Posts: 6
Joined: 14 Nov 2017, 16:22

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 05:15

Odlanir wrote:Some like this ?

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


This does exactly what I need, THANK YOU so very, very, very much! You live you learn, as they say : )))))
User avatar
jeeswg
Posts: 2973
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 09:31

- That text looks like JSON format.
- If you end up doing more with JSON text from the YouTube API, you will find it a lot easier to use a JSON library to convert the string into an object, and refer to object keys. Something like var := obj.videoID.
- It might sound complicated, but it actually makes things a lot easier.
- I had done a lot with InStr/SubStr previously, and it was a relief using arrays instead.

help converting text to CSV - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=33644&p=155919#p155919
Help using json? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34046&p=157734#p157734
User
Posts: 112
Joined: 26 Jun 2017, 08:12

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 10:39

adamas wrote:.


@adamas, this seems to work too:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

User
Posts: 112
Joined: 26 Jun 2017, 08:12

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 10:50

Odlanir wrote:.

adamas wrote:.


@adamas,

The @Odlanir example is not very suitable because regex must match all the text in order to get (KhmrdFsY6Ls)!
(if your original text is too large, it may cause regex limit errors!)

In the other hand, from my example, regex just needs to match ("videoId": "KhmrdFsY6Ls") in order to get (KhmrdFsY6Ls)!
User
Posts: 112
Joined: 26 Jun 2017, 08:12

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 11:08

Odlanir wrote:.

adamas wrote:.


the code below explains what I mentioned in the post above:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

Odlanir
Posts: 47
Joined: 20 Oct 2016, 08:20

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 13:20

User wrote:
The @Odlanir example is not very suitable because regex must match all the text in order to get (KhmrdFsY6Ls)!
(if your original text is too large, it may cause regex limit errors!)


@User: Are you sure ? I've tested my RegEx with a 10MB file and I've found any problem, it performed exactly like your RegEx. Only two different approaches.
Cheers.
User
Posts: 112
Joined: 26 Jun 2017, 08:12

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 13:51

Odlanir wrote:.


So do you really think that forcing regex to match all the 10 MB text only to get "KhmrdFsY6Ls" string is the best way to go?

what if the file is 1 GB? 2, 10 or more? All the 1GB or more file content would be stored in "out[0]" var (unnecessary ram usage and sometimes may cause regex limit error!!!)

in the other hand, using my approach, regex just needs to match and store in "Matched" variable ("videoId": "KhmrdFsY6Ls") in order to get (KhmrdFsY6Ls) string, even if the file is 1TB or more!
Odlanir
Posts: 47
Joined: 20 Oct 2016, 08:20

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 18:14

Your sample is not correct. Try with a file.

Code: [Select all] [Download] GeSHi © Codebox Plus

FileRead, text, test99.txt ; <--- 10MB file
RegExMatch(text, """videoId"".*?""(.*?)""", Matched)
strx := "User example : `n`nMatched`t" Matched "`nMatched1`t" Matched1 "`n`n"

RegExMatch(text, "OmU).*videoId"":\s*?""(.*)"".*?", out)
strx .= "Odlanir example : `n`nout[0]`t" out[0] "`nout[1]`t" out[1]
clipboard := strx

the clipboard contents:
User example :

Matched "videoId": "KhmrdFsY6Ls"
Matched1 KhmrdFsY6Ls

Odlanir example :

out[0] "videoId": "KhmrdFsY6Ls"
out[1] KhmrdFsY6Ls
User
Posts: 112
Joined: 26 Jun 2017, 08:12

Re: RegEx to Match Expression With Quotation Marks

21 Nov 2017, 19:14

Odlanir wrote:.


What sample is not correct my friend?

Read file does not translate `r`n to `n, and since ".*" can't match new lines by default (in this case `r`n), that's why your approach did not match all the 10 MB text file!

but listen, some people chooses to translate any `r`n to `n while reading files, and in this situations your approach would match all the 10MB text from the file!

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

text =
(
"nextPageToken": "CAEQAA",
"regionCode": "US",
"videoId": "KhmrdFsY6Ls"
"pageInfo": {
"totalResults": 245,
"resultsPerPage": 1
)


from the code above, any `r`n is translated to `n before the string is stored in "text" variable, and that's why your approach matches all the "text" string!

If you can't clearly see that my approach is more suitable than yours, well my friend, I can't do anything about that!

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot] and 44 guests