I have looked everywhere I could to find a function to split a string by a regular expression. I've even looked in other languages for it and no luck.
Python has the ability to do this (I think natively) but I haven't seen anyone create a function to do this.
If anyone knows a function that can split a string based on a regular expression, I'd be more than happy.
Split String by RegEx Topic is solved
- Delta Pythagorean
- Posts: 627
- Joined: 13 Feb 2017, 13:44
- Location: Somewhere in the US
- Contact:
Split String by RegEx
[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat
Re: Split String by RegEx
Can you give an example of what you want?
The question is a little vague.
Could you not just use SubStr() ?
I did a search on "split a string using regular expression" and got a host of replies, but there's no way to tell if any of them would help.
The question is a little vague.
Could you not just use SubStr() ?
I did a search on "split a string using regular expression" and got a host of replies, but there's no way to tell if any of them would help.
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Split String by RegEx Topic is solved
Code: Select all
str := "ABCDEFGCVBCRTYECEFGH"
arr := StrSplitRegEx(str, "(?<=b)c|e(?=f)", "i")
for k, v in arr
MsgBox, % v
StrSplitRegEx(str, pattern, options := "") {
arr := []
prevPos := 1, prevLen := 0
while RegExMatch(str, options . "O)" . pattern, m, m ? m.Pos + m.Len : 1) {
arr.Push( SubStr(str, prevPos + prevLen, m.Pos - prevPos - prevLen) )
prevPos := m.Pos, prevLen := m.Len
}
arr.Push( SubStr(str, prevPos + prevLen) )
Return arr
}
Re: Split String by RegEx
Code: Select all
for k, v in RegExSplit("ABCDEFGCVBCRTYECEFGH", "i)(?<=b)c|e(?=f)")
s .= k " -> " v "`n"
MsgBox % RTrim(s)
RegExSplit(ByRef String, Delimiter := "", OmitChars := "", MaxParts := -1) {
static uFFFF := Chr(0xFFFF)
; early exit, split by chars
if (Delimiter = "")
return StrSplit(String, Delimiter, OmitChars, MaxParts)
; has regex flags?
if RegExMatch(Delimiter, "^([^\(\)]*\))(.*)$", m)
Delimiter := m1 "(" m2 ")"
else
Delimiter := "(" Delimiter ")"
return StrSplit(RegExReplace(String, Delimiter, uFFFF), uFFFF, OmitChars, MaxParts)
}
- Delta Pythagorean
- Posts: 627
- Joined: 13 Feb 2017, 13:44
- Location: Somewhere in the US
- Contact:
Re: Split String by RegEx
Surprisingly, yes! That works perfectly!teadrinker wrote: ↑23 May 2020, 21:17Something like this?Code: Select all
str := "ABCDEFGCVBCRTYECEFGH" arr := StrSplitRegEx(str, "(?<=b)c|e(?=f)", "i") for k, v in arr MsgBox, % v StrSplitRegEx(str, pattern, options := "") { arr := [] prevPos := 1, prevLen := 0 while RegExMatch(str, options . "O)" . pattern, m, m ? m.Pos + m.Len : 1) { arr.Push( SubStr(str, prevPos + prevLen, m.Pos - prevPos - prevLen) ) prevPos := m.Pos, prevLen := m.Len } arr.Push( SubStr(str, prevPos + prevLen) ) Return arr }
[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat
- Delta Pythagorean
- Posts: 627
- Joined: 13 Feb 2017, 13:44
- Location: Somewhere in the US
- Contact:
Re: Split String by RegEx
This would imply that I know the position of the string I'm wanting to split by. However I can see how I would use RegExMatch's output to find the position. But the person right below you beat us to the punch.
[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat
- Delta Pythagorean
- Posts: 627
- Joined: 13 Feb 2017, 13:44
- Location: Somewhere in the US
- Contact:
Re: Split String by RegEx
Both swagfag and teadrinker solved this, but teadrinker was first and seems to me that it's the most reliable. If anyone else has a better solution, then by all means do post it.
[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Split String by RegEx
You add an extra subpattern by this, so this may cause confusion.swagfag wrote: ↑Code: Select all
if RegExMatch(Delimiter, "^([^\(\)]*\))(.*)$", m) Delimiter := m1 "(" m2 ")" else Delimiter := "(" Delimiter ")"
Code: Select all
str := "ABCDEFGCVBCRTYECEFGH"
arr := StrSplitRegEx(str, "(?<=cd)(..).*?\1", "i")
for k, v in arr
MsgBox, % v
StrSplitRegEx(str, pattern, options := "") {
arr := []
prevPos := 1, prevLen := 0
while RegExMatch(str, options . "O)" . pattern, m, m ? m.Pos + m.Len : 1) {
arr.Push( SubStr(str, prevPos + prevLen, m.Pos - prevPos - prevLen) )
prevPos := m.Pos, prevLen := m.Len
}
arr.Push( SubStr(str, prevPos + prevLen) )
Return arr
}
Code: Select all
for k, v in RegExSplit("ABCDEFGCVBCRTYECEFGH", "i)(?<=cd)(..).*?\1")
s .= k " -> " v "`n"
MsgBox % RTrim(s)
RegExSplit(ByRef String, Delimiter := "", OmitChars := "", MaxParts := -1) {
static uFFFF := Chr(0xFFFF)
; early exit, split by chars
if (Delimiter = "")
return StrSplit(String, Delimiter, OmitChars, MaxParts)
; has regex flags?
if RegExMatch(Delimiter, "^([^\(\)]*\))(.*)$", m)
Delimiter := m1 "(" m2 ")"
else
Delimiter := "(" Delimiter ")"
return StrSplit(RegExReplace(String, Delimiter, uFFFF), uFFFF, OmitChars, MaxParts)
}
Re: Split String by RegEx
scratch that, see https://www.autohotkey.com/boards/viewtopic.php?p=331034#p331034
Last edited by swagfag on 24 May 2020, 08:01, edited 1 time in total.
Re: Split String by RegEx
https://biga-ahk.github.io/biga.ahk/#/?id=split does this I believe. Though regex patterns currently need to be specified with surrounding / characters
currently no way to specify "i" regex option.
Code: Select all
A := new biga() ; requires https://www.npmjs.com/package/biga.ahk
A.split("a--b-c", "/[\-]+/")
; => ["a", "b", "c"]
currently no way to specify "i" regex option.
Code: Select all
A.split("ABCDEFGCVBCRTYECEFGH", "/(?<=B)C|E(?=F)/")
; => ["AB", "D", "FGCVB", "RTYEC", "FGH"]
Re: Split String by RegEx
actually i dont remember why i put an extra set of parens in there
this seems to work just fine
Code: Select all
RegExSplit(ByRef String, Delimiter := "", OmitChars := "", MaxParts := -1) {
static uFFFF := Chr(0xFFFF)
; early exit, split by chars
if (Delimiter = "")
return StrSplit(String, Delimiter, OmitChars, MaxParts)
return StrSplit(RegExReplace(String, Delimiter, uFFFF), uFFFF, OmitChars, MaxParts)
}
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Split String by RegEx
Yeah, this seems to work
Who is online
Users browsing this forum: NinjoOnline and 290 guests