twistermetal
twistermetal

Reputation: 61

trying rereplace and replace to modify some data

I am suffering from regex illness, i am taking medicines but nothing happening, now i am stuck again with this issue

<cfset Change = replacenocase(mytext,'switch(cSelected) {',' var x = 0;while(x < cSelected.length){switch(cSelected[x]) {','one')>

this did not changed anything

i tried Rereplace too

<cfset Change = rereplacenocase(mytext,'[switch(cSelected) {]+',' var x = 0;while(x < cSelected.length){switch(cSelected[x]) {','one')>

this created weird results

Upvotes: 0

Views: 101

Answers (1)

Regular Jo
Regular Jo

Reputation: 5500

Parentheses, square brackets, and curly brackets are special characters in any implementation of RegEx. Wrapping something in [square brackets] means any of the characters within so [fifty] would match any of f,i,t,y. The plus sign after it just means to match any of these characters as many times as possible. So yes [switch(cSelected) {]+ would replace switch(cSelected) {, but it would also replace any occurrence of switch, or s, or w, or the words this or twitch() because each character in these is represented in your character class.

As a regex, you would instead want (switch\(cSelected\) \{) (the + isn't useful here, and we have to escape the parentheses that we want literally represented. It is also a good idea to escape curly braces because they have special meaning in parts of regex and I believe that when you're new to regex, there's no such thing as over-escaping.

(switch            # Opens Capture Group
                     # Literal switch
 \(cSelected       # Literal (
                     # Literal cSelected
 \)                # Literal )
                     #   single space
 \{                # Literal {
)                  # Closes Capture Group

You can also try something like (switch\(cSelected\)\s*\{), using the token \s* to represent any number of whitespace characters.

(switch            # Opens CG1
                     # Literal switch
 \(cSelected       # Literal (
                     # Literal cSelected
 \)                # Literal )
 \s*               # Token: \s for white space
                     # * repeats zero or more times
 \{                # Literal {
)                  # Closes CG1

What's needed, and the reason people can't be of much assistance is an excerpt from what you're trying to modify and more lines of code.

Potential reasons that the non-regex ReplaceNoCase() isn't working is either that it can't make the match it needs, which could be a whitespace issue, or it could be that you have two variables setting Change to an action based on the mytext variable..

Upvotes: 0

Related Questions