Jump to content

StringRegExp syntax


 Share

Recommended Posts

I have a string,

$Array = "ch2ch3ch3ch4ch7ch1fr22ch2fr21ch3fr34ch4fr10r29rch1ch32ch2ch3ch3ch25ch4ch2ch1fr22ch2fr16ch3fr14c

h4fr28r24rch1ch32ch2ch3ch3ch17ch4ch25"

And i want check three last characters of the string finishs for "r" two digits and "r"

I have tried it :

$Count = StringLen(FileRead(@DesktopDir & "\Test.txt"))
$LastCharacters = StringRegExp(StringTrimLeft(FileRead(@DesktopDir & "\test.txt"),$Count-3),"[r]\d{2}[r]")

And if three last characters are different of "r" two digits and "r", i delete this portion string

thank you

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

...?

Local $sWillGetStripped = "The following will get stripped: ABCD"
Local $sWillNotGetStripped = "The following will not get stripped: r00r"

ConsoleWrite(StripEnd($sWillGetStripped) & @CRLF)
ConsoleWrite(StripEnd($sWillNotGetStripped) & @CRLF)

Func StripEnd($sString)
    If Not StringRegExp($sString, "r\d{2}r$") Then $sString = StringTrimRight($sString, 4)
    Return $sString
EndFunc

Is this what you're looking to do? Your post isn't exactly clear, and your example code doesn't help us figure out what it is you mean. There are too many contradictions between what you said and what you coded.

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

  • Moderators

...?

Local $sWillGetStripped = "The following will get stripped: ABCD"
Local $sWillNotGetStripped = "The following will not get stripped: r00r"

ConsoleWrite(StripEnd($sWillGetStripped) & @CRLF)
ConsoleWrite(StripEnd($sWillNotGetStripped) & @CRLF)

Func StripEnd($sString)
    If Not StringRegExp($sString, "r\d{2}r$") Then $sString = StringTrimRight($sString, 4)
    Return $sString
EndFunc

Is this what you're looking to do? Your post isn't exactly clear, and your example code doesn't help us figure out what it is you mean. There are too many contradictions between what you said and what you coded.

I *think* this can be accomplished with 1 line of RegExReplace
StringRegExpReplace($sString, "(?i)(.+?r\d{2}r\z)|(.+?)(.{4}\z)", "\1\2")
to look like:
Local $sWillGetStripped = "The following will get stripped: ABCD"
Local $sWillNotGetStripped = "The following will not get stripped: r00r"

ConsoleWrite(StripEnd($sWillGetStripped) & @CRLF)
ConsoleWrite(StripEnd($sWillNotGetStripped) & @CRLF)

Func StripEnd($sString)
    Return StringRegExpReplace($sString, "(?i)(.+?r\d{2}r\z)|(.+?)(.{4}\z)", "\1\2")
EndFunc
But, your way is probably how I would have approached it myself.

Edit:

Tested original expression I wrote, it failed... Tested above expression, it does seem to work.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I tried this,

$pattern = "ch2ch3ch3ch4ch7ch1fr22ch2fr21ch3fr34ch4fr10r29rch1ch32ch2ch3ch3ch25ch4ch2ch1fr22ch2fr16ch3fr14ch4fr28r24rch1ch32ch2ch3ch3ch17ch4ch25ch1fr14ch2fr28ch3fr2ch4fr33r25rch1ch32ch2ch17ch3ch33ch4ch23ch1fr24ch2fr13ch3fr1ch4fr6r22rch1ch32ch2ch17ch3ch33ch4ch0ch1fr3ch2fr32ch3fr19ch4fr11r8rch1ch32ch2ch31ch3ch33ch4ch17ch1fr27ch2fr19ch3fr33ch4fr24r34"
StringRegExpReplace($pattern, "(?i)(.+?r\d{2}r\z)|(.+?)(....\z))", "\1\2")
MsgBox(0,"",$pattern)

and the result is the same string...

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

  • Moderators

I tried this,

$pattern = "ch2ch3ch3ch4ch7ch1fr22ch2fr21ch3fr34ch4fr10r29rch1ch32ch2ch3ch3ch25ch4ch2ch1fr22ch2fr16ch3fr14ch4fr28r24rch1ch32ch2ch3ch3ch17ch4ch25ch1fr14ch2fr28ch3fr2ch4fr33r25rch1ch32ch2ch17ch3ch33ch4ch23ch1fr24ch2fr13ch3fr1ch4fr6r22rch1ch32ch2ch17ch3ch33ch4ch0ch1fr3ch2fr32ch3fr19ch4fr11r8rch1ch32ch2ch31ch3ch33ch4ch17ch1fr27ch2fr19ch3fr33ch4fr24r34"
StringRegExpReplace($pattern, "(?i)(.+?r\d{2}r\z)|(.+?)(....\z))", "\1\2")
MsgBox(0,"",$pattern)

and the result is the same string...

Try with the edited version I made before you posted this.

Edit:

You also have 1 too many closing parenthesis in that statement, I probably had that myself before I edited.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Because there was a slight syntax error in the matching pattern (notice the extra closing parenthesis at the end?), which SmOke_N already fixed (Edit: Bah, SmOke_N got to it first :)). Also, StringRegExpReplace() doesn't modify the input string -- it returns the new string, so you're supposed to do something like $pattern = StringRegExpReplace(...) anyway.

@SmOke_N: Yeah, I'm not entirely proficient in regex (though I can usually get my way around given a fair amount of time), so I didn't feel like thinking up anything more than an extremely sparse/simple regular expression ;D

Edit: @jerem488: The only thing you really need to be careful with is the case insensitivity. If you do want it to strip something like "R00R" (because the "R"s are uppercase), then you need to remove the "(?i)" from the regular expression.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

Sorry... but I don't understand I msut put or try....

Local $sWillGetStripped = "The following will get stripped: ABCD"
Local $sWillNotGetStripped = "The following will not get stripped: r00r"

ConsoleWrite(StripEnd($sWillGetStripped) & @CRLF)
ConsoleWrite(StripEnd($sWillNotGetStripped) & @CRLF)

Func StripEnd($sString)
    Return StringRegExpReplace($sString, "(?i)(.+?r\d{2}r\z)|(.+?)(.{4}\z)", "\1\2")
EndFunc

and this code do nothing :)

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

  • Moderators

Sorry... but I don't understand I msut put or try....

Local $sWillGetStripped = "The following will get stripped: ABCD"
Local $sWillNotGetStripped = "The following will not get stripped: r00r"

ConsoleWrite(StripEnd($sWillGetStripped) & @CRLF)
ConsoleWrite(StripEnd($sWillNotGetStripped) & @CRLF)

Func StripEnd($sString)
    Return StringRegExpReplace($sString, "(?i)(.+?r\d{2}r\z)|(.+?)(.{4}\z)", "\1\2")
EndFunc

and this code do nothing :lmao:

The code writes to the bottom pane (console ... Thus "ConsoleWrite") of your SciTe Editor.

Try this:

Local $sWillGetStripped = "The following will get stripped: ABCD"
Local $sWillNotGetStripped = "The following will not get stripped: r00r"

MsgBox(0, "Stripped String", StripEnd($sWillGetStripped))
MsgBox(0, "Not Stripped", StripEnd($sWillNotGetStripped))

Func StripEnd($sString)
    Return StringRegExpReplace($sString, "(?i)(.+?r\d{2}r\z)|(.+?)(.{4}\z)", "\1\2")
EndFunc

Edit:

And Ultima was waiting for that one (Just to beat me!) :)

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

$pattern = "ch2ch3ch3ch4ch7ch1fr22ch2fr21ch3fr34ch4fr10r29rch1ch32ch2ch3ch3ch25ch4ch2ch1fr22ch2fr16ch3fr14ch4fr28r24rch1ch32ch2ch3ch3ch17ch4ch25ch1fr14ch2fr28ch3fr2ch4fr33r25rch1ch32ch2ch17ch3ch33ch4ch23ch1fr24ch2fr13ch3fr1ch4fr6r22rch1ch32ch2ch17ch3ch33ch4ch0ch1fr3ch2fr32ch3fr19ch4fr11r8rch1ch32ch2ch31ch3ch33ch4ch17ch1fr27ch2fr19ch3fr33ch4fr24r34"
$array = StringRegExp($pattern, "(?i)(.+?r\d{2}r\z)|(.+?)(....\z))", "\1\2")
for $i = 0 to UBound($array) - 1
    msgbox(0, "RegExp result" & $i, $array[$i])
Next

don't work....... Why ?

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

Because you're still not reading carefully. StringRegExp() is not StringRegExpReplace(). And once again, that regular expression you're using has a syntax error, so it won't work no matter what function you use it in anyway. SmOke_N's StripEnd() works perfectly well. Why are you avoiding it?

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

So, I go resume.

I have a string (in a tex file) similar to this string "451025478152r12r894251502300154r91r202"

And I want read this file by starting of the end. And while three last character of the file are different of this regular expression ==> "r" (one or two decimal digit) and "r", we delete characters.

So with the string "451025478152r12r894251502300154r91r202"

the final string I must have is "451025478152r12r894251502300154r91r" because the three last character are similar to "r" (one or two decimal digit) and "r".

Thank you in advance

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

  • Moderators

So, I go resume.

I have a string (in a tex file) similar to this string "451025478152r12r894251502300154r91r202"

And I want read this file by starting of the end. And while three last character of the file are different of this regular expression ==> "r" (one or two decimal digit) and "r", we delete characters.

So with the string "451025478152r12r894251502300154r91r202"

the final string I must have is "451025478152r12r894251502300154r91r" because the three last character are similar to "r" (one or two decimal digit) and "r".

Thank you in advance

Do not bump your posts within 24 hours of you last post period. Especially asking the same question that's already been answered, but you refuse to accept it.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

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