How to delete the text of parentheses in a sentence Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
songdg
Posts: 561
Joined: 04 Oct 2017, 20:04

How to delete the text of parentheses in a sentence

Post by songdg » 14 Jan 2021, 02:13

Like "Absorb and integrate (people, ideas, or culture) into a wider society or culture." to become "Absorb and integrate into a wider society or culture."
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to delete the text of parentheses in a sentence

Post by BoBo » 14 Jan 2021, 03:53

RegEx (prefered) or StrSplit() :?:
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: How to delete the text of parentheses in a sentence  Topic is solved

Post by boiler » 14 Jan 2021, 03:57

Code: Select all

str := "Absorb and integrate (people, ideas, or culture) into a wider society or culture."
newStr := RegExReplace(str, "( \(|\()[^\)]*?\)")
MsgBox, % str "`n`n" newStr
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to delete the text of parentheses in a sentence

Post by BoBo » 14 Jan 2021, 05:21

@boiler is back in town :thumbup:
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: How to delete the text of parentheses in a sentence

Post by sofista » 14 Jan 2021, 07:42

As BoBo suggested,

Code: Select all

str := "Absorb and integrate (people, ideas, or culture) into a wider society or culture."
newStr := StrSplit(str, [" (", ")"])
MsgBox, % str "`n`n" newStr[1] newStr[3]
User avatar
Chunjee
Posts: 1419
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to delete the text of parentheses in a sentence

Post by Chunjee » 14 Jan 2021, 16:13

dirty:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

string := "Absorb and integrate (people, ideas, or culture) into a wider society or culture."

msgbox, % A.replace(string, "/\(.+\) /", "")
; => "Absorb and integrate into a wider society or culture."
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: How to delete the text of parentheses in a sentence

Post by boiler » 15 Jan 2021, 04:28

Code: Select all

str := "The solution should probably handle cases (like this one) with multiple pairs of parentheses (as might sometimes occur)."
newStr := RegExReplace(str, "( \(|\()[^\)]*?\)")
MsgBox, % str "`n`n" newStr
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: How to delete the text of parentheses in a sentence

Post by AHKStudent » 15 Jan 2021, 04:35

boiler wrote:
15 Jan 2021, 04:28

Code: Select all

str := "The solution should probably handle cases (like this one) with multiple pairs of parentheses (as might sometimes occur)."
newStr := RegExReplace(str, "( \(|\()[^\)]*?\)")
MsgBox, % str "`n`n" newStr
im learning regex, what is the reason for the |\( It looks to me like a repeat of what is before it. I tried it without it and it worked well, so im wondering what that extra OR \( does?

EDIT: I think I figured it out, the first one covers a ( proceeded by a space and the second one doesnt?

Code: Select all

str := "The solution should probably handle cases (like this one) with multiple pairs of parentheses (as might sometimes occur)."
newStr := RegExReplace(str, "( \()[^\)]*?\)")
MsgBox, % str "`n`n" newStr
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: How to delete the text of parentheses in a sentence

Post by boiler » 15 Jan 2021, 04:42

AHKStudent wrote:
15 Jan 2021, 04:35
what is the reason for the |\( It looks to me like a repeat of what is before it. I tried it without it and it worked well, so im wondering what that extra OR \( does?

EDIT: I think I figured it out, the first one covers a ( proceeded by a space and the second one doesnt?
That is correct.

Code: Select all

str := "It handles cases where no space precedes the open parenthesis(just in case)."
newStr := RegExReplace(str, "( \(|\()[^\)]*?\)")
MsgBox, % str "`n`n" newStr
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: How to delete the text of parentheses in a sentence

Post by AHKStudent » 15 Jan 2021, 06:30

boiler wrote:
15 Jan 2021, 04:42
AHKStudent wrote:
15 Jan 2021, 04:35
what is the reason for the |\( It looks to me like a repeat of what is before it. I tried it without it and it worked well, so im wondering what that extra OR \( does?

EDIT: I think I figured it out, the first one covers a ( proceeded by a space and the second one doesnt?
That is correct.

Code: Select all

str := "It handles cases where no space precedes the open parenthesis(just in case)."
newStr := RegExReplace(str, "( \(|\()[^\)]*?\)")
MsgBox, % str "`n`n" newStr
Thank you
songdg
Posts: 561
Joined: 04 Oct 2017, 20:04

Re: How to delete the text of parentheses in a sentence

Post by songdg » 16 Jan 2021, 00:00

boiler wrote:
14 Jan 2021, 03:57

Code: Select all

str := "Absorb and integrate (people, ideas, or culture) into a wider society or culture."
newStr := RegExReplace(str, "( \(|\()[^\)]*?\)")
MsgBox, % str "`n`n" newStr
You're amazing :bravo:
songdg
Posts: 561
Joined: 04 Oct 2017, 20:04

Re: How to delete the text of parentheses in a sentence

Post by songdg » 16 Jan 2021, 00:02

sofista wrote:
14 Jan 2021, 07:42
As BoBo suggested,

Code: Select all

str := "Absorb and integrate (people, ideas, or culture) into a wider society or culture."
newStr := StrSplit(str, [" (", ")"])
MsgBox, % str "`n`n" newStr[1] newStr[3]
Thank you, much appreciated :dance:
songdg
Posts: 561
Joined: 04 Oct 2017, 20:04

Re: How to delete the text of parentheses in a sentence

Post by songdg » 16 Jan 2021, 00:02

Chunjee wrote:
14 Jan 2021, 16:13
dirty:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

string := "Absorb and integrate (people, ideas, or culture) into a wider society or culture."

msgbox, % A.replace(string, "/\(.+\) /", "")
; => "Absorb and integrate into a wider society or culture."
Thank you, much appreciated :clap:
Post Reply

Return to “Ask for Help (v1)”