compact21

StringSplit exclude values

15 posts in this topic

Example()

Func Example()
    Local $aDays = StringSplit("Mon,Tues,Wed,Thur,Fri.Sat,Sun", ",.") ; Split the string of days using the delimiter "," and the default flag value.
    For $i = 1 To $aDays[0] ; Loop through the array returned by StringSplit to display the individual values.
        MsgBox($MB_SYSTEMMODAL, "", "$aDays[" & $i & "] - " & $aDays[$i])
    Next
EndFunc   ;==>Example

Hello, this is from help file. How do i return all values excluding "Fri" which is delimited by a period?

Share this post


Link to post
Share on other sites



compact21,

You need a RegEx for that:

#include <Array.au3>
#include <StringConstants.au3>

$sText = "Mon,Tues,Wed,Thur,Fri.Sat,Sun"

Example()

_NoPeriod()

Func Example()
    Local $aDays = StringSplit($sText, ",.") ; Split the string of days using the delimiter "," and the default flag value.
    _ArrayDisplay($aDays, "StringSplit", Default, 8)
EndFunc   ;==>Example

Func _NoPeriod()
    $aDays = StringRegExp($sText, "(\w{3,4})(?:,|\z)", $STR_REGEXPARRAYGLOBALMATCH)
    _ArrayDisplay($aDays, "RegEx", Default, 8)
EndFunc

RegEx decode:

(\w{3,4}) - capture all sequences of 3 or 4 letters
(?:,|\z)  - followed by a comma or the end of the string, but do not capture this delimiter

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

A variation   :)

#Include <Array.au3>

Local $sDays = "Mon,Tues,Wed,Thur,Fri.Sat,Sun"
Local $aDays = StringRegExp($sDays, '\w+(?=,|$)', 3)

_ArrayDisplay($aDays)

This one means : "get all sequences of word chars followed by a comma or end of string"

Share this post


Link to post
Share on other sites

Ha, Melba and mikell beat me to it. I remove the unwanted day using StringRegExpReplace() before using StringSplit(). The regular expression is different: removing anything ending with a period. It serves as another illustration of the numerous possible ways there are to do things.
 

Example()

Func Example()
    Local $sString = StringRegExpReplace("Mon,Tues,Wed,Thur,Fri.Sat,Sun", '[^,]+\.', '') ; remove xxxx.
    Local $aDays = StringSplit($sString, ",") ; Split the string of days using the delimiter "," and the default flag value.
    For $i = 1 To $aDays[0] ; Loop through the array returned by StringSplit to display the individual values.
        MsgBox($MB_SYSTEMMODAL, "", "$aDays[" & $i & "] - " & $aDays[$i])
    Next
EndFunc   ;==>Example

 

Share this post


Link to post
Share on other sites

Or if you have already determined what the string is that needs to be removed, just split on it in its entirety

#include<array.au3>

$sText = "Mon,Tues,Wed,Thur,Fri.Sat,Sun"

$aText = stringsplit($sText , "Fri." , 3)

msgbox(0, '' , _ArrayToString($aText , ""))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thank you M23. Exactly what i needed. 

Edited by compact21

Share this post


Link to post
Share on other sites
53 minutes ago, Melba23 said:

compact21,

You need a RegEx for that:

#include <Array.au3>
#include <StringConstants.au3>

$sText = "Mon,Tues,Wed,Thur,Fri.Sat,Sun"

Example()

_NoPeriod()

Func Example()
    Local $aDays = StringSplit($sText, ",.") ; Split the string of days using the delimiter "," and the default flag value.
    _ArrayDisplay($aDays, "StringSplit", Default, 8)
EndFunc   ;==>Example

Func _NoPeriod()
    $aDays = StringRegExp($sText, "(\w{3,4})(?:,|\z)", $STR_REGEXPARRAYGLOBALMATCH)
    _ArrayDisplay($aDays, "RegEx", Default, 8)
EndFunc

RegEx decode:

(\w{3,4}) - capture all sequences of 3 or 4 letters
(?:,|\z)  - followed by a comma or the end of the string, but do not capture this delimiter

M23

A tip how to use StringRegExp when delimiter is "?" instead of ","   ? Thakn you.

Share this post


Link to post
Share on other sites

Just escape the question mark with a backslash => \?

Share this post


Link to post
Share on other sites
#include<array.au3>

$delim = "?"

;~ $sText = "Mon,Tues,Wed,Thur,Fri.Sat,Sun"
$sText = "Mon,Tues,Wed?Thur,Fri,Sat,Sun"

$aText = stringsplit(stringreverse(stringregexpreplace(stringreverse($sText) , "(\" & $delim & "\D+?)," , ",")) , "," , 2)

_ArrayDisplay($aText)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

yup I was backwards, I totally thought he asked what if it was '?' instead of '.' 

\Q....\E would make my regex life so much easier if I remembered to use them, but working on the reversed string is still my favorite way to provide examples :)

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
1 hour ago, iamtheky said:

.. working on the reversed string is still my favorite way to provide examples :)

I admit that this could be efficient but isn't it a slightly twisted way ?   :)

Share this post


Link to post
Share on other sites

but its an easy win for the recipient to fix and then they have to reverse the regex which means they did something more than copy/paste.  And if you see that crap from anyone but me, you know where they got it from.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Irrefutable argument ... hat off   :D

Share this post


Link to post
Share on other sites

Thanks a lot!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now