Regex Help for a Needle Containing Quotation Marks

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
zayntheboss
Posts: 15
Joined: 24 Feb 2019, 15:21

Regex Help for a Needle Containing Quotation Marks

13 Oct 2023, 18:17

I've been racking my brains for hours on this to no avail.

Here's a bit of the string I'm working with. It's a huge chunk of characters on a single line.

Code: Select all

{"__ref":"UmF0aW5nLTI0NzIyODY4"}},"UmF0aW5nLTI0NzIyODY4":{"__id":"UmF0aW5nLTI0NzIyODY4","__typename":"Rating","comment":"HORRIBLE PROF the lectures arent testable material at all and the TAs are hard markers ","flagStatus":"UNFLAGGED","createdByUser":false,"teacherNote":null,"legacyId":24722868,"date":"2015-05-02 18:09:13 +0000 UTC","class":"3DD3","helpfulRating":1,"clarityRating":1,"isForOnlineClass":false,"difficultyRating":5,"attendanceMandatory":"","wouldTakeAgain":null,"grade":"","textbookUse":1,"isForCredit":true,"ratingTags":"","id":"UmF0aW5nLTI0NzIyODY4","adminReviewedAt":"2015-05-05 22:13:59 +0000 UTC","thumbsUpTotal":0,"thumbsDownTotal":1,"thumbs":{"__refs":["VGh1bWItMjI2OTkwOA=="]}},"VGh1bWItMjI2OTkwOA==":
I want to trim away any occurrences of a specific part, as specified by this regex expression:

Code: Select all

\"Rating\".*?ratingTags
. This expression works as I want when I test it using regex101.com

When I put it into RegexMatch using this expression:

Code: Select all

match := RegexMatch(e, """Rating"".*?ratingTags", ratings)
msgbox % ratings.Length()
I get nothing in the messagebox.

Please, any help would be appreciated.


[Mod action: Moved topic to the v1 section since this is v1 code. The main section is for v2.]
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: Regex Help for a Needle Containing Quotation Marks

13 Oct 2023, 18:56

Length() does not give the length of a string. It gives the length of an array in terms of its largest integer index. The match variable ratings contains a string, not an object. If you want the length of that, use this:

Code: Select all

MsgBox, % StrLen(ratings)

And of course, you can see the contents of the variable itself like this:

Code: Select all

MsgBox, % ratings

By the way, you posted in the main section which is now for v2 code. I moved your thread to the v1 section. Please post in the appropriate section going forward.
sofista
Posts: 654
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Regex Help for a Needle Containing Quotation Marks

13 Oct 2023, 19:59

@zayntheboss I am a bit confused, because if you want to remove some part of a string, you don't use RegExMatch. Anyway, maybe this is the outcome you want

Code: Select all

e =
(
{"__ref":"UmF0aW5nLTI0NzIyODY4"}},"UmF0aW5nLTI0NzIyODY4":{"__id":"UmF0aW5nLTI0NzIyODY4","__typename":"Rating","comment":"HORRIBLE PROF the lectures arent testable material at all and the TAs are hard markers ","flagStatus":"UNFLAGGED","createdByUser":false,"teacherNote":null,"legacyId":24722868,"date":"2015-05-02 18:09:13 +0000 UTC","class":"3DD3","helpfulRating":1,"clarityRating":1,"isForOnlineClass":false,"difficultyRating":5,"attendanceMandatory":"","wouldTakeAgain":null,"grade":"","textbookUse":1,"isForCredit":true,"ratingTags":"","id":"UmF0aW5nLTI0NzIyODY4","adminReviewedAt":"2015-05-05 22:13:59 +0000 UTC","thumbsUpTotal":0,"thumbsDownTotal":1,"thumbs":{"__refs":["VGh1bWItMjI2OTkwOA=="]}},"VGh1bWItMjI2OTkwOA==":

)

RegexMatch(e, """Rating.*?ratingTags""", ratings)
MsgBox, % ratings

/* Output:

"Rating","comment":"HORRIBLE PROF the lectures arent testable material at all and the TAs are hard markers ","flagStatus":"UNFLAGGED","createdByUser":false,"teacherNote":null,"legacyId":24722868,"date":"2015-05-02 18:09:13 +0000 UTC","class":"3DD3","helpfulRating":1,"clarityRating":1,"isForOnlineClass":false,"difficultyRating":5,"attendanceMandatory":"","wouldTakeAgain":null,"grade":"","textbookUse":1,"isForCredit":true,"ratingTags"

 */

;Or this other:

MsgBox, % RegExReplace(e, ".*\K""Rating.*?ratingTags""")

/* Output:

{"__ref":"UmF0aW5nLTI0NzIyODY4"}},"UmF0aW5nLTI0NzIyODY4":{"__id":"UmF0aW5nLTI0NzIyODY4","__typename"::"","id":"UmF0aW5nLTI0NzIyODY4","adminReviewedAt":"2015-05-05 22:13:59 +0000 UTC","thumbsUpTotal":0,"thumbsDownTotal":1,"thumbs":{"__refs":["VGh1bWItMjI2OTkwOA=="]}},"VGh1bWItMjI2OTkwOA==":

 */

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Draken, oktavimark and 334 guests