Regular Expression Help please!

G

Giles

My (VB/ASP) site parses pseudocode created by authors. For example, the
author's HTML might contain
[start small padded box] This content displays in a box [end small padded
box]
The bits in square brackets are then replaced with appropriate HTML to
create a border around the text.

PageHTML=replace(PageHTML,"[start small padded box]","<div
style='width:200px; padding:4px; border:1px solid #000'>")
PageHTML=replace(PageHTML,"[end small padded box]","</div>")

The problem is, the spaces might (or might not) be &nbsp; due to the
authoring interface
[start small&nbsp;padded box] This content displays in a box [end&nbsp;small
padded&nbsp;box]

Is there a regular expression that can turn &nbsp; (if they exist) into
spaces? (Prior to applying the pseudocode conversion)

function deNBSP(s,html)
?
?
end function
PageHTML=deNBSP("[start small padded box]", PageHTML)
PageHTML=deNBSP("[end small padded box]", PageHTML)

The pseudocode phrases can be quite long, and have a lot of spaces, I was
hoping a RegExp would be quicker than looping through, using replace() for
every permutation of space - &nbsp; in a phrase.

Thanks if you can help, or advise a different strategy.
 
B

Bob Barrows

Giles said:
The problem is, the spaces might (or might not) be &nbsp; due to the
authoring interface
[start small&nbsp;padded box] This content displays in a box
[end&nbsp;small padded&nbsp;box]

Is there a regular expression that can turn &nbsp; (if they exist)
into spaces? (Prior to applying the pseudocode conversion)
..

A simple call to Replace should do this - no need for regex.
s = Replace(s,"&nbsp;", " ")
 
G

Giles

Giles said:
The problem is, the spaces might (or might not) be &nbsp; due to the
authoring interface
[start small&nbsp;padded box] This content displays in a box
[end&nbsp;small padded&nbsp;box]

Is there a regular expression that can turn &nbsp; (if they exist)
into spaces? (Prior to applying the pseudocode conversion)
.
Bob Barrows wrote:

A simple call to Replace should do this - no need for regex.
s = Replace(s,"&nbsp;", " ")

Thanks Bob, but that would change all the nbsp's in the PageHTML, not just
the ones in the pseudocode phrases. The page may contain other necessary
nbsp's. It's the pseudocode phrase that varies:
I am trying to find a way around doing -
PageHTML=replace(PageHTML,"[start&nbsp;small padded box]","[start small
padded box]")
PageHTML=replace(PageHTML,"[start small&nbsp;padded box]","[start small
padded box]")
PageHTML=replace(PageHTML,"[start small padded&nbsp;box]","[start small
padded box]")
PageHTML=replace(PageHTML,"[start&nbsp;small&nbsp;padded box]","[start small
padded box]")
PageHTML=replace(PageHTML,"[start small&nbsp;padded&nbsp;box]","[start small
padded box]")
PageHTML=replace(PageHTML,"[start&nbsp;small&nbsp;padded&nbsp;box]","[start
small padded box]")
 
B

Bob Barrows

Giles said:
Giles said:
The problem is, the spaces might (or might not) be &nbsp; due to the
authoring interface
[start small&nbsp;padded box] This content displays in a box
[end&nbsp;small padded&nbsp;box]

Is there a regular expression that can turn &nbsp; (if they exist)
into spaces? (Prior to applying the pseudocode conversion)
.
Bob Barrows wrote:

A simple call to Replace should do this - no need for regex.
s = Replace(s,"&nbsp;", " ")

Thanks Bob, but that would change all the nbsp's in the PageHTML, not
just the ones in the pseudocode phrases. The page may contain other
necessary nbsp's. It's the pseudocode phrase that varies:
I am trying to find a way around doing -
PageHTML=replace(PageHTML,"[start&nbsp;small padded box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start small&nbsp;padded box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start small padded&nbsp;box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start&nbsp;small&nbsp;padded
box]","[start small padded box]")
PageHTML=replace(PageHTML,"[start
small&nbsp;padded&nbsp;box]","[start small padded box]")
PageHTML=replace(PageHTML,"[start&nbsp;small&nbsp;padded&nbsp;box]","[start
small padded box]")

Then you will need regex. Unfortunately, I'm not fluent in regular
expressions so all I can do is suggest you go to the documentation.
Hopefully someone else will jump in and help you out.
 
E

Evertjan.

Bob Barrows wrote on 01 nov 2009 in
microsoft.public.inetserver.asp.general:
Giles said:
Giles wrote:

The problem is, the spaces might (or might not) be &nbsp; due to
the authoring interface
[start small&nbsp;padded box] This content displays in a box
[end&nbsp;small padded&nbsp;box]

Is there a regular expression that can turn &nbsp; (if they exist)
into spaces? (Prior to applying the pseudocode conversion)
.
Bob Barrows wrote:

A simple call to Replace should do this - no need for regex.
s = Replace(s,"&nbsp;", " ")

Thanks Bob, but that would change all the nbsp's in the PageHTML, not
just the ones in the pseudocode phrases. The page may contain other
necessary nbsp's. It's the pseudocode phrase that varies:
I am trying to find a way around doing -
PageHTML=replace(PageHTML,"[start&nbsp;small padded box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start small&nbsp;padded box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start small padded&nbsp;box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start&nbsp;small&nbsp;padded
box]","[start small padded box]")
PageHTML=replace(PageHTML,"[start
small&nbsp;padded&nbsp;box]","[start small padded box]")
PageHTML=replace(PageHTML,"[start&nbsp;small&nbsp;padded&nbsp;box]","[
start small padded box]")

Then you will need regex. Unfortunately, I'm not fluent in regular
expressions so all I can do is suggest you go to the documentation.
Hopefully someone else will jump in and help you out.

Perhaps I can help you out with Regex,
but I do not know what you mean by "pseudocode phrases".

Let us just define a string called PageHTML [i am not interested in the
final purpose], I suppose pars of that string with well defined start and
ends need to be purged of a certain substring.

Please define the start and end of such substrings.
 
G

Giles

Evertjan. said:
Bob Barrows wrote on 01 nov 2009 in
microsoft.public.inetserver.asp.general:
Giles said:
Giles wrote:

The problem is, the spaces might (or might not) be &nbsp; due to
the authoring interface
[start small&nbsp;padded box] This content displays in a box
[end&nbsp;small padded&nbsp;box]

Is there a regular expression that can turn &nbsp; (if they exist)
into spaces? (Prior to applying the pseudocode conversion)
.
Bob Barrows wrote:

A simple call to Replace should do this - no need for regex.
s = Replace(s,"&nbsp;", " ")

Thanks Bob, but that would change all the nbsp's in the PageHTML, not
just the ones in the pseudocode phrases. The page may contain other
necessary nbsp's. It's the pseudocode phrase that varies:
I am trying to find a way around doing -
PageHTML=replace(PageHTML,"[start&nbsp;small padded box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start small&nbsp;padded box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start small padded&nbsp;box]","[start
small padded box]")
PageHTML=replace(PageHTML,"[start&nbsp;small&nbsp;padded
box]","[start small padded box]")
PageHTML=replace(PageHTML,"[start
small&nbsp;padded&nbsp;box]","[start small padded box]")
PageHTML=replace(PageHTML,"[start&nbsp;small&nbsp;padded&nbsp;box]","[
start small padded box]")

Then you will need regex. Unfortunately, I'm not fluent in regular
expressions so all I can do is suggest you go to the documentation.
Hopefully someone else will jump in and help you out.

Perhaps I can help you out with Regex,
but I do not know what you mean by "pseudocode phrases".

Let us just define a string called PageHTML [i am not interested in the
final purpose], I suppose pars of that string with well defined start and
ends need to be purged of a certain substring.

Please define the start and end of such substrings.

Thank you Evertjan
Each pseudocode phrase is a sub-string within PageHTML that starts with
Open-Square-Bracket [, and ends with Close-Square-Bracket, ].
It can contain any number of words, separated by spaces.
Some of the "spaces" might be &nbsp;
It needs to be purged of &nbsp; each occurrence being replaced by a space.

e.g.
[word1 word2 word3 word4] - is OK
[word1&nbsp;word2] - needs converting to [word1 word2]

Examples are
[podcast lecture.mp3]

[movie /flv/demo.flv width=400 height=300]

<b>Quiz</b><br />
[mcq start]
Questions here...
[mcq end]
 
E

Evertjan.

Giles wrote on 01 nov 2009 in microsoft.public.inetserver.asp.general:
Thank you Evertjan
Each pseudocode phrase is a sub-string within PageHTML that starts
with Open-Square-Bracket [, and ends with Close-Square-Bracket, ].
It can contain any number of words, separated by spaces.
Some of the "spaces" might be &nbsp;
It needs to be purged of &nbsp; each occurrence being replaced by a
space.

Could be done like this,
I use a Javascript function for simplicity:

==============================================
<% 'vbs

PageHTML = "[word1 word2 word3 word4]z&nbsp;z" &_
"[word5&nbsp;word6]z&nbsp;z" &_
"[word7&nbsp;word8&nbsp;word9]"

PageHTML = replaceNbsp(PageHTML)

Response.write PageHTML

%>

<script language='javascript' runat='server'>
function replaceNbsp(s) {
return s.replace(/(\[.*?\])/g,function(a)
{return a.replace(/&nbsp;/g,' ');});
};
</script>
==============================================

You will need view-source to see that the &nbsp;
outside the [...] are not touched.
 
G

Giles

Giles wrote on 01 nov 2009 in microsoft.public.inetserver.asp.general:
Thank you Evertjan
Each pseudocode phrase is a sub-string within PageHTML that starts
with Open-Square-Bracket [, and ends with Close-Square-Bracket, ].
It can contain any number of words, separated by spaces.
Some of the "spaces" might be &nbsp;
It needs to be purged of &nbsp; each occurrence being replaced by a
space.

Could be done like this,
I use a Javascript function for simplicity:

==============================================
<% 'vbs

PageHTML = "[word1 word2 word3 word4]z&nbsp;z" &_
"[word5&nbsp;word6]z&nbsp;z" &_
"[word7&nbsp;word8&nbsp;word9]"

PageHTML = replaceNbsp(PageHTML)

Response.write PageHTML

%>

<script language='javascript' runat='server'>
function replaceNbsp(s) {
return s.replace(/(\[.*?\])/g,function(a)
{return a.replace(/&nbsp;/g,' ');});
};
</script>
==============================================

You will need view-source to see that the &nbsp;
outside the [...] are not touched.

Perfect. Your help is very much appreciated, thank you Evertjan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top