regexreplace help Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

regexreplace help

01 Jan 2023, 02:05

Code: Select all

Isotherms & Range of temperature: (at 9.17 AM)
Isotopes : (at 8.14 AM)
temperature inversion: (at 9.17 AM)
the above string is in my clipboard
i need to remove the brackets with time.
there are many such strings with different time.

any regex experts ?

what i tried so far

Code: Select all

clipboard := RegExReplace(clipboard, "U)\(.*\)")
but this will remove everything in brackets

what i am trying to do is
Remove only if it finds AM or PM inside brackets
any idea ?
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: regexreplace help

01 Jan 2023, 03:11

I'm no expert, but this should work.

This makes the search fairly specific, yet flexible enough for any time value. It will accommodate a period or colon separator, and optional space(s) between time value and AM/PM. It also removes the space in front of the parenthesis. You can add the U option if you like, but for your example string above, it's not necessary since the needle below does not contain (.*).

Code: Select all

clipboard := RegExReplace(clipboard, "i) \(at \d{1,2}(\.|:)\d{2} *(AM|PM)\)")
Last edited by andymbody on 01 Jan 2023, 03:41, edited 1 time in total.
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: regexreplace help

01 Jan 2023, 03:41

thank you, this works for some pages

some other pages has time written like this (9.24 AM) and (9:24 AM) without an "at"
what shall we do with that.
Last edited by ananthuthilakan on 01 Jan 2023, 03:49, edited 1 time in total.
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: regexreplace help

01 Jan 2023, 03:42

some other pages has time written like this (9.24 AM) and (9:24 AM)
what shall we do with that
I updated the code above to handle colon as well
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: regexreplace help

01 Jan 2023, 03:51

yes ! thank you
still at some pages the time is showing like this (9:20 AM) with out the word "at" inside.
they are not getting replaced
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: regexreplace help  Topic is solved

01 Jan 2023, 03:57

ananthuthilakan wrote:
01 Jan 2023, 03:51
still at some pages the time is showing like this (9:20 AM) with out the word "at" inside.
they are not getting replaced
ok... this should accommodate with or without "at" and any type of horizontal white space

Code: Select all

clipboard := RegExReplace(clipboard, "i)\h*\((at)?\h*\d{1,2}(\.|:)\d{2}\h*(AM|PM)\)")

; this one will also allow military time - AM/PM optional
clipboard := RegExReplace(clipboard, "i)\h*\((at)?\h*\d{1,2}(\.|:)\d{2}\h*(AM|PM)*\)")
BTW... this would be a more accurate needle for the purists (using ?: within non-capturing parenthesis)... I think it will use less resources, and might be slightly faster, BUT is many times more confusing to look at for anyone not familiar with it.

Code: Select all

clipboard := RegExReplace(clipboard, "i)\h*\((?:at)?\h*\d{1,2}(?:\.|:)\d{2}\h*(?:AM|PM)*\)")
Last edited by andymbody on 01 Jan 2023, 04:43, edited 1 time in total.
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: regexreplace help

01 Jan 2023, 04:06

Thank you for your help!
this works great
where could i learn this regex thing ?
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: regexreplace help

01 Jan 2023, 04:12

ananthuthilakan wrote:
01 Jan 2023, 04:06
where could i learn this regex thing ?
These are very good resources to start.

viewtopic.php?f=7&t=28031

https://www.autohotkey.com/docs/v1/lib/RegExMatch.htm

https://www.autohotkey.com/docs/v1/misc/RegEx-QuickRef.htm#Options

Also, experimenting is a good way to learn... The syntax is slightly different on this site, but it's a good way to experiment

https://regex101.com/

Here is a AHK tester app to play with

viewtopic.php?f=6&t=77133

RegexBuddy -
Really nice tool. Able to test regex expressions/needles and show the internal steps taken step-by-step, and why a needle works or doesn't
Well worth the $40

https://www.regexbuddy.com/
Last edited by andymbody on 02 Jan 2023, 02:11, edited 1 time in total.
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: regexreplace help

01 Jan 2023, 05:17

Explanation in case you're interested...

Code: Select all


clipboard := RegExReplace(clipboard, "i)\h*\((?:at)?\h*\d{1,2}(?:\.|:)\d{2}\h*(?:AM|PM)*\)")
/*
	i)				case-insensitive
	\h*				0 or more horz whitespace chars (optional horz ws)
	\(				left/opening parenthesis (mandatory)
	(?:at)?			"?:" means non-capture group, holding optional "at", the trailing ? means 0 or 1 "at"
	\h*				0 or more horz whitespace chars (optional horz ws)
	\d{1,2}			digit(s) - minimum of 1 digit, max of 2 digits (mandatory)
	(?:\.|:)		"?:" means non-capture group, holding dot (\.) OR (|) colon (:) (mandatory)
	\d{2}			digits - minimum of 2 digits, max of 2 digits (mandatory)
	\h*				0 or more horz whitespace chars (optional horz ws)
	(?:AM|PM)*		"?:" means non-capture group, holding AM OR (|) PM, the trailing * means that the AM|PM is optional
	\)				right/closing parenthesis (mandatory)
	
	So, what is non-capture group? 
	
	Normally, when you place something in parenthesis, you are "asking" AHK to capture
	the value of the contents of the parenthesis, so you can recall and use the value later.
	Kind of like using the parenthesis to create a built-in on-the-fly variable that contains the
	value of what is captured within the parenthesis. You would then be able to recall the value
	by specifying $1 (acts as a variable) for the first capture group, $2 for the second capture
	group, etc. But using the "?:" within the parenthesis tells AHK not to bother with the
	capture function, since you don't plan to recall the value that would normally be captured.
	This make the parenthesis a non-capture group, or just a way to logically group the contents for
	things like OR processing and/or adding the trailing * or ? that will act on what is inside the parenthesis.
	
	I hope this makes sense.
*/
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: regexreplace help

01 Jan 2023, 06:32

...and why not just? TempClip:= RegexReplace(Temp, "[()]", "") Have an expert explain it to me... he just wants to delete the symbols ()

Code: Select all

Temp:=
(
"Isotherms & Range of temperature: (at 9.17 AM)
Isotopes : (at 8.14 AM)
temperature inversion: (at 9.17 AM)"
)
TempClip:= RegexReplace(Temp, "[()]", "")
MsgBox, % TempClip
Reedited:

Oh, i understand, if you are in very long texts, you may not want to delete the parentheses in other cases.
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: regexreplace help

01 Jan 2023, 11:14

wetware05 wrote:
01 Jan 2023, 06:32
he just wants to delete the symbols ()
My understanding was that they wanted to remove parenthesis and contents within, if the contents are time. If they just wanted to remove the parenthesis surrounding time, then below are a couple ways. These are just possibilities - I'm sure there are other ways, that the "true experts" could present.

In this case we take advantage of a capture group to remove the entire match, then put just the contents (time) back using the $1 on-the-fly "variable".

Code: Select all


; specific - am/pm is optional, to allow for military time as well

clipboard := RegExReplace(clipboard, "i)\h*\((?:at)?\h*(\d{1,2}(?:\.|:)\d{2}\h*(?:AM|PM)*)\)", " $1")

; OR
; less specific, using just AM or PM as match factor for time (am/pm no longer optional)
; NOTE - the less specific version can cause false-positives
;	for instance it would also remove parenthesis from (spam)

clipboard := RegExReplace(clipboard, "i)\h*\(([^\)]+(?:AM|PM))\)", " $1") ; explanation below

/*
	i)				case-insensitive (for am/pm or AM/PM)
	\h*				optional horz whitespaces
	\(				left/opening parenthesis
	(				begin capture group 1
	[^\)]+			anything (+ means mandatory - 1 or more chars) that is NOT a right/closing parenthesis...
	(?:AM|PM)		... up to and including AM or PM (am/pm also mandatory - no longer optional)
	)				close capture group 1
	\)				right/close parenthesis
	" $1"			replace with a space and what was captured in group1 (essentially just removing parenthesis from match) 
*/
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: regexreplace help

01 Jan 2023, 15:42

andymbody wrote:
01 Jan 2023, 11:14
wetware05 wrote:
01 Jan 2023, 06:32
he just wants to delete the symbols ()
My understanding was that they wanted to remove parenthesis and contents within, if the contents are time. If they just wanted to remove the parenthesis surrounding time, then below are a couple ways. These are just possibilities - I'm sure there are other ways, that the "true experts" could present.

In this case we take advantage of a capture group to remove the entire match, then put just the contents (time) back using the $1 on-the-fly "variable".

Code: Select all


; specific - am/pm is optional, to allow for military time as well

clipboard := RegExReplace(clipboard, "i)\h*\((?:at)?\h*(\d{1,2}(?:\.|:)\d{2}\h*(?:AM|PM)*)\)", " $1")

; OR
; less specific, using just AM or PM as match factor for time (am/pm no longer optional)
; NOTE - the less specific version can cause false-positives
;	for instance it would also remove parenthesis from (spam)

clipboard := RegExReplace(clipboard, "i)\h*\(([^\)]+(?:AM|PM))\)", " $1") ; explanation below

/*
	i)				case-insensitive (for am/pm or AM/PM)
	\h*				optional horz whitespaces
	\(				left/opening parenthesis
	(				begin capture group 1
	[^\)]+			anything (+ means mandatory - 1 or more chars) that is NOT a right/closing parenthesis...
	(?:AM|PM)		... up to and including AM or PM (am/pm also mandatory - no longer optional)
	)				close capture group 1
	\)				right/close parenthesis
	" $1"			replace with a space and what was captured in group1 (essentially just removing parenthesis from match) 
*/
hi, andymbody

I corrected it in a few minutes and reedited the post. I was wrong.
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: regexreplace help

02 Jan 2023, 10:28

andymbody wrote:
01 Jan 2023, 05:17
Explanation in case you're interested...

Code: Select all


clipboard := RegExReplace(clipboard, "i)\h*\((?:at)?\h*\d{1,2}(?:\.|:)\d{2}\h*(?:AM|PM)*\)")
/*
	i)				case-insensitive
	\h*				0 or more horz whitespace chars (optional horz ws)
	\(				left/opening parenthesis (mandatory)
	(?:at)?			"?:" means non-capture group, holding optional "at", the trailing ? means 0 or 1 "at"
	\h*				0 or more horz whitespace chars (optional horz ws)
	\d{1,2}			digit(s) - minimum of 1 digit, max of 2 digits (mandatory)
	(?:\.|:)		"?:" means non-capture group, holding dot (\.) OR (|) colon (:) (mandatory)
	\d{2}			digits - minimum of 2 digits, max of 2 digits (mandatory)
	\h*				0 or more horz whitespace chars (optional horz ws)
	(?:AM|PM)*		"?:" means non-capture group, holding AM OR (|) PM, the trailing * means that the AM|PM is optional
	\)				right/closing parenthesis (mandatory)
	
	So, what is non-capture group? 
	
	Normally, when you place something in parenthesis, you are "asking" AHK to capture
	the value of the contents of the parenthesis, so you can recall and use the value later.
	Kind of like using the parenthesis to create a built-in on-the-fly variable that contains the
	value of what is captured within the parenthesis. You would then be able to recall the value
	by specifying $1 (acts as a variable) for the first capture group, $2 for the second capture
	group, etc. But using the "?:" within the parenthesis tells AHK not to bother with the
	capture function, since you don't plan to recall the value that would normally be captured.
	This make the parenthesis a non-capture group, or just a way to logically group the contents for
	things like OR processing and/or adding the trailing * or ? that will act on what is inside the parenthesis.
	
	I hope this makes sense.
*/
this is a great explanation to get started with :bravo:
thankyou

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], macromint, Spawnova and 350 guests