Jump to content

Need help replacing repetitive chars in a string.


Recommended Posts

I have a situation, I have various strings that look like this...

 

  1. .....................15533.............291928....E
  2. .....1....................428............E
  3. ...........2999191929281990....................299919192881...................................E
  4. etc

and so forth.

 

There is a pattern to the madness... It's basically a random amount of "." followed by something not "." followed by a random amount of "." followed by something not "." and so forth.

I want to turn all the repetitive ............ into just 1 .

So .....................15533.............291928....E becomes .15533.291928.E

Solving this riddle will allow me to use a stringtrim with the . as the delimiter and allow me to just extract the 15533 and 291928 from the array.

Anyone know how to convert all series of ... into just 1 . ? I can't solve it because the string has two series of .............. in the 1 string. and since the ........... varies, I use that as the delimiter as it will throw off my array value.

 

I hope my question makes sense! Thank you coders for your brain work. I wish I had the ability to solve this, it took me hours of searching other topics just to get this far.

Edited by Darkflame808
Link to comment
Share on other sites

$str = ".....................15533.............291928....E"
;~ $str = ".....1....................428............E"
;~ $str =  "...........2999191929281990....................299919192881...................................E"

msgbox(0, '' , stringregexpreplace($str , "(\.+)" , "."))

 

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

Link to comment
Share on other sites

If you are going to use StringSplit() function to split the string on the single dots to get the numbers into an array.   It might be easier to by-pass the dots and go straight for the numbers.
But be warned, you will be coming face to face with the much maligned Regular Expressions.  
Although, these two examples are a simple and easy introduction.
For more info on Regular Expressions see the StringRegExp function in the AutoIt help file.

#include <Array.au3> ; Used only to display array.

$str = ".....................15533.............291928....E"
;~ $str = ".....1....................428............E"
;~ $str =  "...........2999191929281990....................299919192881...................................E"

;Repeating iamtheky's example
MsgBox(0, '', StringRegExpReplace($str, "\.+", ".")) ; Match 1 or more groups of literal dots, "\.+", and replace with 1 dot.

;Or

$array = StringRegExp($str, "\d+", 3) ; Match 1 or more groups of digits, "\d+", and return in an array. Parameter "3" - $STR_REGEXPARRAYGLOBALMATCH  - Return array of global matches.
_ArrayDisplay($array)

 

Link to comment
Share on other sites

what about this?

;~ $str = ".....................15533.............291928....E"
;~ $str = ".....1....................428............E"
$str =  "...........2999191929281990....................299919192881...................................E"


do
$str = stringreplace($str , ".." , "." , 0)
until @extended = 0

msgbox(0, '' , $str)

 

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

Link to comment
Share on other sites

Thank you everyone for your assistance!

I tried the solutions in order as posted and iamtheky's first post worked a treat!

I've spent so much hours rewriting the same script in different ways trying to attack my problem from new angles. Once I applied your fix it was smooth sailing from there.

I am very much grateful for your assistance!

Link to comment
Share on other sites

Thanks,

I think this pulls it off on a single line without loops or regex, provided that none of the values are similar, otherwise its going to jack that up.

#include<array.au3>

$str = ".....................15533.............291928....E"
;~ $str = ".....1....................428............E"
;~ $str =  "...........2999191929281990....................299919192881...................................E"


msgbox(0, '' , _ArrayToString(_ArrayUnique(stringsplit($str , "." , 2)) , "." , 1))

 

Edited by iamtheky

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

Link to comment
Share on other sites

3 hours ago, iamtheky said:

I think this pulls it off on a single line without loops or regex, provided that none of the values are similar, otherwise its going to jack that up.

...just another bizzarre "one line" way

#include <array.au3>

$str = ".....................15533.............291928....E"
;~ $str = ".....1....................428............E"
;~ $str =  "...........2999191929281990....................299919192881...................................E"

$aGroups = StringSplit(StringStripWS(StringReplace($str, ".", " "), 7), " ", 2)

_ArrayDisplay($aGroups)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...