Page 1 of 1
help with regular expression
Posted: Sat Aug 23, 2003 12:04 am
by lohroffe
I need to do a search & replace in my html as follows (except the code isn't showing up correctly in this post - the " is actually the ANSI character & # 3 4 ;):
"<designer>.*</span>"
removing <designer> and </span> to yield
".*"
examples:
"<designer>barbecue</span>"
replaced by "barbecue"
AND
"<designer>I don't know what you're talking about!</span>"
replaced by "I don't know what you're talking about!"
AS WELL AS hundreds of other permutations (103 instances in 139 files, to be specific)
Suggestions??
I can't really do it in 2 chunks, because there are other places where my end /span is NOT preceded by <designer> and I don't want to replace ALL of my </span>"
Thanks!
Posted: Sat Aug 23, 2003 7:21 am
by bbadmin
You can store text matched by parts of a regular expression in a numbered register, by enclosing that part in parentheses. Each matched pair of parentheses stores the matched text in the next register. If you are using the default regular expression syntax, the parentheses must be escaped with backslashes, whereas with POSIX syntax, literal parentheses must be escaped. The registers can be referred to in the replacement expression using \1 to \9.
For example, using POSIX syntax:
Find what: <designer>(.*)</span>
Replace with: \1
Keith MacDonald
Helios Software Solutions
Posted: Sat Aug 23, 2003 5:26 pm
by lohroffe
Thanks for your reply - I have 2 more questions, though.
1. How does it know which syntax I'm using? I did the statement as you suggested and it returned only text containing a literal pair of parentheses.
I added \ before the parens and it worked just fine.
What lets it know if I'm using POSIX or not?
2. I didn't state it explicitly in my question, but I'm struggling with the ampersands.
Before I find the cases of <designer> </span> preceded and followed by everything, I need to find the cases of
& q u o t ; <designer>(.*)</span> & q u o t ;
(i added the spaces in the &; phrase to get it to display properly here)
\(&*\)<designer>\(.*\)</span>\(&*\)
gets me the same results as
<designer>\(.*\)</span>
and
\(\&*\)<designer>\(.*\)</span>\(\&*\)
gets me "invalid regular expression"
Suggestions?
TIA
Posted: Sat Aug 23, 2003 7:28 pm
by Bob Hansen
POSIX or not?
From the Main Menu select, Configure, Preferences. Left Click on Editor.
There is an option to "Use POSIX regular expression syntax." I think the default is unchecked, not using POSIX.
So, you can try this for Non-POSIX replacements:
===================================
Search for:
<designer>\(.*\)</span>
Replace with
\1
=========================
Explanation of Search string:
<designer> = defined string
\( = beginning of first defined expression
. = any character
* = any number of previous characters (.)
\) = end of first defined expression
</span> = defined string
======================
Explanation of Replace string:
\1 = first defined expression
=======================
Common English explanation"
Find any string that is preceeded by <designer> and followed by</span> and remover the surrounding strings
That worked on the following test lines:
<designer>this is a test</span>
<designer>so is this</span>
<designer>me too</span>
<designer></span>
<designer>last line had nothing</span>
If you want to keep double quotes ", then this will keep any existing double quotes.
If you want to remove existing double quotes, then include them in the Search String like this:
Search for:
"<designer>\(.*\)</span>"
Replace with:
\1
========================
Hope this helps...............good luck,
Bob