Jump to content

RegExp - Remove white spaces before and after coma


RAMzor
 Share

Recommended Posts

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 by RAMzor
Link to comment
Share on other sites

  • Developers

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.
  :)

Link to comment
Share on other sites

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)

 

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

Link to comment
Share on other sites

awww 💩

#include <Array.au3>

local $a[0] , $s = "TDM111A5,      RCT222Y5/ 7  ; FDT444E4 /8 , ABC222R5"

_ArrayDisplay(StringSplit(StringStripWS($s , 4) , ",;" , 2))

 

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

Link to comment
Share on other sites

suppose I'd continue to ignore them like i did last night 😕

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

Link to comment
Share on other sites

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 by Malkey
See comments in my example.
Link to comment
Share on other sites

  • 2 weeks later...

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
 Share

×
×
  • Create New...