RAMzor Posted October 16, 2019 Posted October 16, 2019 (edited) Hi guys I need your help. I have string like this : "TDM111A5, RCT222Y5/ 7 ; FDT444E4 /8 , ABC222R5" I need find a coma or semicolon and delete white spaces before and after them The output should be a string and/or array String : "TDM111A5,RCT222Y5/ 7;FDT444E4 /8,ABC222R5" Array: TDM111A5 RCT222Y5/ 7 FDT444E4 /8 ABC222R5 Edited October 16, 2019 by RAMzor
InnI Posted October 16, 2019 Posted October 16, 2019 $s = "TDM111A5, RCT222Y5/ 7 ; FDT444E4 /8 , ABC222R5" $s = StringRegExpReplace($s, "\h*([,;])\h*", "\1") ; string ConsoleWrite($s & @CRLF) $a = StringSplit($s, ",;") ; array #include <Array.au3> _ArrayDisplay($a)
Developers Jos Posted October 16, 2019 Developers Posted October 16, 2019 Something like this? $In = "TDM111A5, RCT222Y5/ 7 ; FDT444E4 /8 , ABC222R5" $Out = StringRegExpReplace($In,"\s*([,;])\s*","$1") ConsoleWrite($out &@CRLF) Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
mikell Posted October 16, 2019 Posted October 16, 2019 Straight-to-array version #include <Array.au3> $s = "TDM111A5, RCT222Y5/ 7 ; FDT444E4 /8 , ABC222R5" $a = StringRegExp($s, "\h*([^,;]+)\h*", 3) _ArrayDisplay($a) FrancescoDiMuro 1
iamtheky Posted October 16, 2019 Posted October 16, 2019 an irregular straight to array version #include <Array.au3> local $a[0] , $s = "TDM111A5, RCT222Y5/ 7 ; FDT444E4 /8 , ABC222R5" _ArrayAdd($a , StringReplace(StringStripWS($s , 4) , "," , ";") , "" , ";") _ArrayDisplay($a) FrancescoDiMuro 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
iamtheky Posted October 17, 2019 Posted October 17, 2019 awww 💩 #include <Array.au3> local $a[0] , $s = "TDM111A5, RCT222Y5/ 7 ; FDT444E4 /8 , ABC222R5" _ArrayDisplay(StringSplit(StringStripWS($s , 4) , ",;" , 2)) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted October 17, 2019 Posted October 17, 2019 7 hours ago, iamtheky said: awww 💩 In both cases, what about the persistent white space just before the 2nd, 3rd and 4th items ?
iamtheky Posted October 17, 2019 Posted October 17, 2019 suppose I'd continue to ignore them like i did last night 😕 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Malkey Posted October 18, 2019 Posted October 18, 2019 (edited) On 10/17/2019 at 3:48 AM, mikell said: Straight-to-array version #include <Array.au3> $s = "TDM111A5, RCT222Y5/ 7 ; FDT444E4 /8 , ABC222R5" $a = StringRegExp($s, "\h*([^,;]+)\h*", 3) _ArrayDisplay($a) Note the trailing "\h*" in mikell's RE pattern does not match any spaces, because all trailing spaces are already captured by previous "([^,;]+)" capture group. This means the 2nd and 3rd items have trailing spaces. Here is another straight-to-array example. #include <Debug.au3> $s = " TDM111A4 , RCT222Y5/ 7 ; FDT444E4 /8 , ABC222R56 " ; $a = StringRegExp($s, "\h*[;,]?(\w+\h*/?\h*\d*)\h*", 3) ; When "/?\h*\d*" does not match in the test string, ; "(\w+\h*" captures trailing spaces in 1st and 4th items. $a = StringRegExp($s, "\h*[;,]?(\w+(?:\h*/\h*\d*)?)\h*", 3) _DebugArrayDisplay($a) Edited October 20, 2019 by Malkey See comments in my example.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now