Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

REGEX find and replace not working

New Here ,
Mar 07, 2022 Mar 07, 2022

I am trying to use a regex to find and replace in Dreamweaver.
My Regex works fine in the 'find' section of the find and replace dialogue box.

 

<th style="[^"]*" class="[^"]*"

In various documentation I have seen the use of $ variables to take the result of the regex and use it in the replace section of the dialogue.

<th style="$2" class="$1"

I cannot get the replace working!

883
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 07, 2022 Mar 07, 2022

I notice three possible errors in your initial expression.

 

First of all, you have to remove the string portion "<th " from the regular expression and ask DW to perform the search within the TH tag

Lena_0-1646656898957.png

 

Secondly, the error comes from the fact that your query is composed of a single string that will necessarily return only one result.
In fact, written like this, the words style and class are an integral part of your search, so you must isolate the pseudo values that would be assigned to them in order to

...
Translate
Community Expert ,
Mar 07, 2022 Mar 07, 2022

I notice three possible errors in your initial expression.

 

First of all, you have to remove the string portion "<th " from the regular expression and ask DW to perform the search within the TH tag

Lena_0-1646656898957.png

 

Secondly, the error comes from the fact that your query is composed of a single string that will necessarily return only one result.
In fact, written like this, the words style and class are an integral part of your search, so you must isolate the pseudo values that would be assigned to them in order to consider them as the groups we are looking for.
The use of parentheses is therefore necessary to materialize this isolation.
So, instead of using the pattern

 

style="[^"]*" class="[^"]*"

 

It is therefore necessary to write

 

style="([^"]*)" class="([^"]*)"

 

This will give you the two returns distinctly identifiable by $1 and $2

 

Finally, this pre-request assumes that the two parts of the string are necessarily written in this order (style... then class..., and not class... then style....). For this reason, we will say that this is the case, and hope (for now) that it is the way it is.

But we also assume that the two parts are separated by only one space. This can be corrected by slightly modifying the query, and adding a [ ]*


So, instead of writing

 

style="([^"]*)" class="([^"]*)"

 

we can write

 

style="([^"]*)"[ ]*class="([^"]*)"

 

 

So, as a result, for the final REGEX in the search field you can write

 

style="([^"]*)"[ ]*class="([^"]*)"

 

And in the replacement field

 

style="$2" class="$1"

 

Lena_1-1646657288619.png

My English being what it is, I imagine that my explanations may not be very clear, but the code should work. Anyway, don't hesitate.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 07, 2022 Mar 07, 2022

Lena,

Thank you for the reply. Your explanation is incredibly useful and makes a lot of sense. I very much appreciate you taking time to explain.

I am now implementing correct REGEX search and replace functionality and using it to tidy up and maintain my code.

Much appreciated.

Kind regards

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2022 Mar 08, 2022
LATEST

Thanks for your reply, I'm glad that it helped you !

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines