Jump to content

How to use StringRegExpReplace to replace non-standard strings


Recommended Posts

Hi guys

I have a list of words that I need to replace certain characters in

there are three different sections that need to be replaced

these are the characters that need to be replaced:

1."/'" to be replaced by "%27"

2."+" to be replaced by "%2B"

3." (" to be replaced by "+(" <- there is a space before the bracket that needs to be replaced by a +

$slashdash = "/'"
$plus = "+"
$spacebracket = " ("

$input = StringRegExpReplace($input, "/'", "%27") ;works
$input = StringRegExpReplace($input, $plus, "%2B") ;doesn't work
$input = StringRegExpReplace($input, $spacebracket, "+(");doesn't work

I have hundreds of words in the list for example

wood\'brown = wood%27brown

brick+2 = brick%2B2

slate (new) = slate+(new)

so each one needs to be run through all three of the StringRegExpReplace

my question is how can I get 2. and 3. to

hope that makes sense and someone can help

many thanks

laurie

Edited by llolslim
Link to comment
Share on other sites

Those are special characters within the regEx engine and as such should be escaped either with a "\" or the\Q\E sequence

$input = StringRegExpReplace($input, "\Q" & $plus & "\E", "%2B")
$input = StringRegExpReplace($input, "\Q" & $spacebracket & "\E", "+(")

EDIT:

I only showed the one method but you might prefer the other one so here it is.

$slashdash = "/'"
$plus = "\+"
$spacebracket = " \("

$input = StringRegExpReplace($input, "/'", "%27")
$input = StringRegExpReplace($input, $plus, "%2B")
$input = StringRegExpReplace($input, $spacebracket, "+(")
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

Those are special characters within the regEx engine and as such should be escaped either with a "\" or the\Q\E sequence

$input = StringRegExpReplace($input, "\Q" & $plus & "\E", "%2B")
$input = StringRegExpReplace($input, "\Q" & $spacebracket & "\E", "+(")

EDIT:

I only showed the one method but you might prefer the other one so here it is.

$slashdash = "/'"
$plus = "\+"
$spacebracket = " \("

$input = StringRegExpReplace($input, "/'", "%27")
$input = StringRegExpReplace($input, $plus, "%2B")
$input = StringRegExpReplace($input, $spacebracket, "+(")

escaping with / is much easier for me to read, so that's great, thank you

From

http://www.regular-expressions.info/characters.html

that makes it very clear as well, so that's a great help also, thank you.
Link to comment
Share on other sites

Glad it worked.

You can easily test regExps with the toolkit in my signature.

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

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