Opened 8 years ago
Closed 8 years ago
#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:1 by , 8 years ago
comment:2 by , 8 years ago
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 , 8 years ago
You mean like this:
#include <Array.au3>
$aRet = StringRegExp("abc", ".", 3)
_ArrayDisplay($aRet, "", Default, 8)
M23
comment:4 by , 8 years ago
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 , 8 years ago
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 , 8 years ago
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 , 8 years ago
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 , 8 years ago
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 , 8 years ago
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 , 8 years ago
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 , 8 years ago
| Resolution: | → Rejected |
|---|---|
| Status: | new → closed |

as in StringRegExp() you mean?
Jos