Modify

Opened 6 years ago

Closed 6 years ago

#3645 closed Feature Request (Rejected)

StringRegExpSplit

Reported by: genius257 <hot415@…> 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 Changed 6 years ago by Jos

as in StringRegExp() you mean?

Jos

comment:2 Changed 6 years ago by genius257 <hot415@…>

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 Changed 6 years ago by Melba23

You mean like this:

#include <Array.au3>
$aRet = StringRegExp("abc", ".", 3)
_ArrayDisplay($aRet, "", Default, 8)

M23

comment:4 Changed 6 years ago by genius257 <hot415@…>

In a small, simple example, yes.

StringRegExpSplit("|a
|b c| d|", "\s*\|\|\s*")

[ "|a", "|b", "c|", "d|" ]
Or

StringRegExpSplit("|a[ "|a", "
|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 Changed 6 years ago by genius257 <hot415@…>

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 Changed 6 years ago by Melba23

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 Changed 6 years ago by genius257 <hot415@…>

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 Changed 6 years ago by Jos

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 Changed 6 years ago by genius257

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 Changed 6 years ago by jchd18

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 Changed 6 years ago by Melba23

  • Resolution set to Rejected
  • Status changed from new to closed

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.