Jump to content

StringRegExpReplace comma from right to left


ALFJ
 Share

Recommended Posts

Hi everyone,

I am still a newbie in the expressions area so I am looking for some help

I am trying to trim a string using StringRegExpReplace but not sure how to go about it, I have seen StringTrimRight ( "string", count ) however the text I am extraction is not always going to have the same amount of text towards the right so this wont work. I have been Experimenting with StringRegExpReplace with all the various things however I have a lot to learn.

$string = "text1,Text2,Text3,Text4,Text5"

I want to extract "just Text1, Text2, Text3" from the string

The text is coming from a file which separates values with the comma ","

The file has multiple lines so I will just need to take the first 3 values of each line and move on to the next line.

Text1 Will always be 5 digits followed by a comma ,

Text2 always varies in lenght but is always followed by a comma ,

Text3 Will always be 2 Characters followed by a comma ,

Text4 always varies in lenght but is always followed by a comma ,

Text5 always varies in lenght and is always at the end.

So the $result would be something like $result = "Text1, Text2, Text3"

I am trying to avoid arrays :P I can probably get it to work with an array however the file is big so I am trying to avoid an array :x . would just like to know how to get it from one line, don't have to go overboard in your code :shifty:

Edited by ALFJ
Link to comment
Share on other sites

I'm sure there's a way to do it with StringRegExp, but that's beyond my SRE abilities.

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

Global $sStr = "Text1, Text2, Text3, Text4, Text5"
$sStr = StringMid($sStr, 1, StringInStr($sStr, ",", 0, 3) - 1)
ConsoleWrite($sStr & @CRLF) ; Text1, Text2, Text3
This works great, thanks!

Maybe someone will stop by and show us the StringRegExpReplace way just to see how it's done.

Even this is a bit complicated how you combine StringMid with StringInstr I can see how the StringInStr works but the StringMid I have never used cause I guess I have always thought of it being for taking from the Middle not the Right or the Left but guess I was wrong lol, guess I have to start thinking about using multiple string functions together to get what I am looking for, I guess that's been my problem I was just thinking one string function at a time.

Edited by ALFJ
Link to comment
Share on other sites

I can't delete the reply so I'll be back with a revision later.

Are the values all on one line as shown or are they on separate lines?

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I can't delete the reply so I'll be back with a revision later.

Are the values all on one line as shown or are they on separate lines?

All values are in the same line, I see someone else did an example already, going to try that one.
Link to comment
Share on other sites

Unless I don't understand this should be a start:

$string ="12345,Text2,Tk,Text4,Text5"
$result = StringRegExp($string, '(?i)(?-s)^\s*(\d{5},.*?,[a-z]{2})',2)
MsgBox(0, "Results", $result[0])

see attached for explanation

Great another great example, and thanks for the explanation file, I will have to take a closer look.

ConsoleWrite("Results " & $result[0] & @LF)

Question will the $result[0] always return the whole result string in the array?

I know it does in this example, just wondering since I didn't read anything as such on the samples in Autoit, it only shows for loops combined with an array to get the result which could add more to the overall code lines.

If $result at [0] assuming there is a match always returns the whole line, then this

might work great for future things for me, just need to get a little more involved with the Expressions part to learn them.

Edited by ALFJ
Link to comment
Share on other sites

If there is only one regex in parentheses, (\d{5},.*?,[a-z]{2}) in this case, yes if found it'll be in $results[0]

any (?...) types don't count they are regex mode modifiers. But I must say using the array code as per the examples for StringRegExp is the best way to go:

for $i = 0 to UBound($result) - 1
    msgbox(0, "RegExp Test with Option 2 - " & $i, $result[$i])
Next
Edited by Jury
Link to comment
Share on other sites

If there is only one regex in parentheses, (\d{5},.*?,[a-z]{2}) in this case, yes if found it'll be in $results[0]

any (?...) types don't count they are regex mode modifiers. But I must say using the array code as per the examples for StringRegExp is the best way to go:

for $i = 0 to UBound($result) - 1
    msgbox(0, "RegExp Test with Option 2 - " & $i, $result[$i])
Next
Yeah Your above example also works even though it returns the values in the 0 & 1 part of the array.

What I like though is those expressions that are 1 line coded such as the example below (which btw doesn't work for the code I was trying to use).

Example:

$Result = StringRegExpReplace($input, $regex, "$1")

However for me to use that I have to learn how to search and how to go about replacing what I had before with my results.

Edited by ALFJ
Link to comment
Share on other sites

If @Jurys epression worked for you then you should also be able to get $result with an SRE Replacement

Not tested but try

$result = StringRegExp($string, "(?i)(?-s)(?m:^)\s*(\d{5},.*?,[a-z]{2}).*(?:\v|$)+","")

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

$result = StringRegExp($string, "(?i)(?-s)(?m:^)\s*(\d{5},.*?,[a-z]{2}).*(?:\v|$)+","")

I tried the above and didn't get the result unless I did something wrong but then again it wasn't tested. Jury's examples did work although since he used the \d{5} in the first part if it didn't have a # there it gave me an error but I am sure that can easily be replaced with something like \w* or [a-z]{5}.

Thanks for all the feedback and help. I am still not great at those 2 functions due to the "patterns" which are new to me, I have to learn those like I know the loops and arrays, I ended up just using extraction with what I know just cause I want to finish what I am currently working on faster and just finish LoL, I will spend more time learning that later, but examples do help learn a little more.

Anyway here's what I did with an array, since I realized it was actually a better way to go since I could probably pull any text from any one of those places without breaking my head trying to figure out how with the patterns lol, I didn't remove the console writes so the code looks longer than it should and no error implementation.

$string = "Text1, Text2, Text3, Text4, Text5"

$string = StringReplace($string," ","",0,0);Trim any spaces
ConsoleWrite($string & @LF);Text1,Text2,Text3,Text4,Text5

$aString = Stringsplit($string, ",",0);Split the values into an array
ConsoleWrite($aString[0] & @LF) ;5 (Count of the values in the array)
ConsoleWrite($aString[1] & @LF) ;Text1
ConsoleWrite($aString[2] & @LF) ;Text2
ConsoleWrite($aString[3] & @LF) ;Text3
ConsoleWrite($aString[4] & @LF) ;Text4
ConsoleWrite($aString[5] & @LF) ;Text5

$newString = $aString[1] & ", " & $aString[2] & ", " & $aString[3] ;Text1, Text2, Text3
ConsoleWrite($newString & @LF)
Edited by ALFJ
Link to comment
Share on other sites

What about this version:

$string = "text1,Text2,Text3,Text4,Text5" & @LF & _
                "12345,Text2,Tk,Text4,Text5" & @LF & _
                "txt23,3423423,23,2352352,238532,sdgukskghks,sdfkshgks,sdjghsgk" & @LF & _
                "balal,bdsljsd,348kjhtwke,34itziwtu,wetuin4"
$test = StringRegExpReplace($string, "(?U:(.*),(.*),(.*)),.*", "$1, $2, $3")
MsgBox(0, "Test", $test)

Happy new year!

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

What about this version:

$string = "text1,Text2,Text3,Text4,Text5" & @LF & _
                "12345,Text2,Tk,Text4,Text5" & @LF & _
                "txt23,3423423,23,2352352,238532,sdgukskghks,sdfkshgks,sdjghsgk" & @LF & _
                "balal,bdsljsd,348kjhtwke,34itziwtu,wetuin4"
$test = StringRegExpReplace($string, "(?U:(.*),(.*),(.*)),.*", "$1, $2, $3")
MsgBox(0, "Test", $test)

Happy new year!

UEZ

This is exactly what I was looking for UEZ!!!

I can avoid the arrays now that I can use this example to lets say pull out 1, 2 or more values.

I was actually messing with something similar earlier but the $1, part was what was confusing me and not knowing much about the advanced characters I wasn't sure where my mistakes were, but with this example you provided, I just added an extra ,(.*) $1, $2, $3, $4 for the return and to extract 4 instead of 3.

This example makes more sense to me now, since I was actually trying it with something similar to this earlier but being a newbie in the area :x I wasn't sure if I was even doing it right So I had given up on trying to figure it out for the day and just did it the way I know how to and not the way I wanted to learn how to.

But thanks to that example, It gave me more hope, and the extra @LF you added on there also helps me understand doing it for multiple rows.

$string = "Text1, Text2, Text3, Text4, Text5"
$string = StringReplace($string, " ", "") 
$test = StringRegExpReplace($string, "(?i:(.*),(.*),(.*),(.*),(.*))", "$1");Text1
$test = StringRegExpReplace($string, "(?i:(.*),(.*),(.*),(.*),(.*))", "$2");Text2
$test = StringRegExpReplace($string, "(?i:(.*),(.*),(.*),(.*),(.*))", "$3");Text3
$test = StringRegExpReplace($string, "(?i:(.*),(.*),(.*),(.*),(.*))", "$4");Text4
$test = StringRegExpReplace($string, "(?i:(.*),(.*),(.*),(.*),(.*))", "$5");Text5

or using your method which is much cleaner especially if the line lets say has a lot of items in it:

$string = "Text1, Text2, Text3, Text4, Text5"
$string = StringReplace($string, ", ", ",") ;Remove any spaces after a comma
$test = StringRegExpReplace($string, "(?U:(.*)),.*", "$1");Text1
$test = StringRegExpReplace($string, "(?U:(.*),(.*)),.*", "$2");Text2
$test = StringRegExpReplace($string, "(?U:(.*),(.*),(.*)),.*", "$3");Text3
$test = StringRegExpReplace($string, "(?U:(.*),(.*),(.*),(.*)),.*", "$4") ;Text4
$test = StringRegExpReplace($string, "(?U:(.*),(.*),(.*),(.*),(.*))", "$5") ;Text5

Yep I can get with this the Text I want without using an array!!

Thanks and Happy New Year to you as well!

Edited by ALFJ
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...