Sign in to follow this  
Followers 0
llolslim

How to use StringRegExpReplace to replace non-standard strings

5 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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!"

Share this post


Link to post
Share on other sites

From

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

If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1\+1=2. Otherwise, the plus sign will have a special meaning.

Share this post


Link to post
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.

Share this post


Link to post
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!"

Share this post


Link to post
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
Sign in to follow this  
Followers 0