#3645 closed Feature Request (Rejected)
StringRegExpSplit
| Reported by: | Owned by: | ||
|---|---|---|---|
| Milestone: | Component: | AutoIt | |
| Version: | Severity: | None | |
| Keywords: | Cc: |
Description
A core function to split strings by regular expression.
Attachments (0)
Change History (11)
comment:2 by , on Jul 17, 2018 at 6:52:52 AM
Yes and no.
Yes that it's by using regular expression.
No because StringRegExp returns the part of string matched by the pattern.
For example StringRegExpSplit("abc", "(:?)") would return ["a", "b", "c"]
comment:3 by , on Jul 17, 2018 at 10:34:14 AM
You mean like this:
#include <Array.au3>
$aRet = StringRegExp("abc", ".", 3)
_ArrayDisplay($aRet, "", Default, 8)
M23
comment:4 by , on Jul 17, 2018 at 5:05:07 PM
In a small, simple example, yes.
| |b | c| | d|", "\s*\|\|\s*") |
[ "|a", "|b", "c|", "d|" ]
Or
| |b | c| | d|", "\s*(\|\|)\s*") |
| ", "|b", " | ", "c|", " | ", "d|" ] |
Or
StringRegExpSplit("Hello 1 word. Sentence number 2.", "(\d)")
[ "Hello ", "1", " word. Sentence number ", "2", "." ]
comment:5 by , on Jul 17, 2018 at 5:09:43 PM
Well it seems i forgot the code block
Here's the examples with the code block
StringRegExpSplit("|a|| |b|| c| ||d|", "\s*\|\|\s*")
[ "|a", "|b", "c|", "d|" ]
StringRegExpSplit("|a|| |b|| c| ||d|", "\s*(\|\|)\s*")
[ "|a", "||", "|b", "||", "c|", "||", "d|" ]
StringRegExpSplit("|a|| |b|| c| ||d|", "(\d)")
[ "Hello ", "1", " word. Sentence number ", "2", "." ]
comment:6 by , on Jul 18, 2018 at 9:40:05 AM
The first 2 are easy to do with the existing function:
#include <Array.au3>
;StringRegExpSplit("|a|| |b|| c| ||d|", "\s*\|\|\s*")
;[ "|a", "|b", "c|", "d|" ]
$aRet = StringRegExp("|a|| |b|| c| ||d|", "(..)(?:\s?\|\||\z)", 3)
_ArrayDisplay($aRet, "", Default, 8)
;StringRegExpSplit("|a|| |b|| c| ||d|", "\s*(\|\|)\s*")
;[ "|a", "||", "|b", "||", "c|", "||", "d|" ]
$aRet = StringRegExp(StringStripWS("|a|| |b|| c| ||d|", 8), "(..)(\|\||\z)", 3)
_ArrayDisplay($aRet, "", Default, 8)
No idea about the last one as the given input does not match the required output in any way.
M23
comment:7 by , on Jul 18, 2018 at 11:28:56 AM
Yeah, the last one was meant to be
StringRegExpSplit("Hello 1 word. Sentence number 2.", "(\d)")
[ "Hello ", "1", " word. Sentence number ", "2", "." ]
And i do get that StringRegExp CAN solve this, it simply seems like a ease of use function.
Especially when converting from one language (like js or php) to AutoIt
comment:8 by , on Jul 18, 2018 at 10:59:53 PM
Something like this?
#include <Array.au3>
$aRet = StringRegExpSplit("|a|| |b|| c| ||d|", "\s*\|\|\s*")
_ArrayDisplay($aRet, "", Default, 8)
;[ "|a", "|b", "c|", "d|" ]
$aRet = StringRegExpSplit("Hello 1 word. Sentence number 2.", "(\d)")
;~ [ "Hello ", "1", " word. Sentence number ", "2", "." ]
_ArrayDisplay($aRet, "", Default, 8)
$aRet = StringRegExpSplit("|a|| |b|| c| ||d|", "\s*(\|\|)\s*")
;[ "|a", "||", "|b", "||", "c|", "||", "d|" ]
_ArrayDisplay($aRet, "", Default, 8)
func StringRegExpSplit($input,$regex)
$a = StringRegExp($input, "(.*?)"& $regex & "|[\S]+", 3)
return $a
EndFunc
Jos
comment:9 by , on Jul 19, 2018 at 1:03:14 AM
Hmmm seems to work for almost everything, except the following.
#include <Array.au3>
$aRet = StringRegExpSplit("abc", "(?:)")
;expected [ "a", "b", "c" ]
;actual [ "", "a", "", "b", "", "c", "" ]
_ArrayDisplay($aRet, "", Default, 8)
func StringRegExpSplit($input,$regex)
$a = StringRegExp($input, "(.*?)"& $regex & "|[\S]+", 3)
return $a
EndFunc
But the chances for that to give me problems currently are slim :)
comment:10 by , on Jul 19, 2018 at 1:27:03 AM
It can still work:
$aRet = StringRegExpSplit("|a|| |b|| c| ||d|", "\s*\|\|\s*")
_ArrayDisplay($aRet, "", Default, 8)
;[ "|a", "|b", "c|", "d|" ]
$aRet = StringRegExpSplit("Hello 1 word. Sentence number 2.", "(\d)")
;~ [ "Hello ", "1", " word. Sentence number ", "2", "." ]
_ArrayDisplay($aRet, "", Default, 8)
$aRet = StringRegExpSplit("|a|| |b|| c| ||d|", "\s*(\|\|)\s*")
;[ "|a", "||", "|b", "||", "c|", "||", "d|" ]
_ArrayDisplay($aRet, "", Default, 8)
$aRet = StringRegExpSplit("abc", "")
;[ "a", "b", "c" ]
_ArrayDisplay($aRet, "", Default, 8)
Func StringRegExpSplit($input,$regex)
Return StringRegExp($input, "(.+?)"& $regex & "|[\S]+", 3)
EndFunc
But maybe you'll tell us that if the subject has adjacent separators it won't again work the way you expect it to.
Note that since the function is an actual one-liner, there is no real purpose for a new function: it's simpler to code it inline with the ad-hoc pattern suited to the case being handled.
Also Trac isn't the place to discuss various regexes; better post a topic in help and when & if a new function emerges from discussion then it'll be time to place a feature request here.
comment:11 by , on Jul 19, 2018 at 8:21:51 AM
| Resolution: | → Rejected |
|---|---|
| Status: | new → closed |

as in StringRegExp() you mean?
Jos