Jump to content

Help with StringRegExp or StringReplace


Recommended Posts

So I've been racking my brain for over a week about this, but I am just not familiar enough with the matching characters or the functions 'StringRegExpReplace' or 'StringReplace'.

Here is an example of a string that I wanna work with.

2011-01-19 01:07:35 PLACEMENT   Users   123456789ABCDEFG Broadcast: Shout: c=12345, i=123456789ABCDEFG, t=44523, a=1111

I want to be able to replace the words between [2011-01-19 01:07:35] and [users].

Basically the word PLACEMENT. I can't use direct string replace. Here is the catch, I need the script to be able to do it all the time, even if the words between [users] and [2011-01-19 01:07:35] are different each time (With spaces, different length). It should be able to figure out whats between the two and replace it with the characters "#####".

Is there any way StringRegExpReplace or StringReplace can do that?

I tried to play with the matching characters and using \h, but i just couldn't get it.

Link to comment
Share on other sites

Does this do what you want?

$sTest = "2011-01-19 01:07:35 PLACEMENT   Users   123456789ABCDEFG Broadcast: Shout: c=12345, i=123456789ABCDEFG, t=44523, a=1111"

ConsoleWrite(StringRegExpReplace($sTest,"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ).*?( Users)","$1#####$2") & @CRLF)

Edit: You could also rely on the word that has to be replaced always has exactly 20 characters before it, after which you'd just have to look for "Users"

If it is possible for "Users" to appear in the part that has to be replaced you might need to extend to pattern to include a larger part of the string.

Edited by Tvern
Link to comment
Share on other sites

$sTest = $sTest = "2011-01-19 01:07:35 Replace all of this part   Users   123456789ABCDEFG Broadcast: Shout: c=12345, i=123456789ABCDEFG, t=44523, a=1111"
$sTest = StringRegExpReplace($sTest, "(?m:^)(?i)([\d\-:]+\h*).+(\h*Users.+)(?:\v|$)", "$1 Replaced with this text $2")

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 tried to play with the matching characters and using \h, but i just couldn't get it.

This example is based on the replacement string being the third "\H+" after the second "\h+".

Local $sTest = "2011-01-19 01:07:35  PLACEMENT  Users   123456789ABCDEFG Broadcast: Shout: c=12345, i=123456789ABCDEFG, t=44523, a=1111"
Local $sReplacementString = "#####"
Local $sNewString = StringRegExpReplace($sTest, "^(\H+\h\H+)\h+\H+\h+(\H+)\h+", "$1 " & $sReplacementString & " $2 ")

ConsoleWrite($sNewString & @CRLF)
MsgBox(0, "Results", $sNewString)
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...