Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista Tutorial - regular expressions to replace but keep character?

Reply
 
Old 07-15-2008   #1 (permalink)
Nick B
Guest


 
 

regular expressions to replace but keep character?

Is there a way to use Replace with Regular Expressions but keep a character
in the original string? A simplified example: If we have "< a >" or "<b >"
or "< br>" within a string and we want to remove the spaces, but keep the
character, is there a way this can be done? A Regular Expression can find
the substring with something like "<\s*[a-z]\s*>" (only finds one character),
but the ReplaceWith argument appears to be static. Is there any way to keep
the character?

Thanks!,
Nick

My System SpecsSystem Spec
Old 07-15-2008   #2 (permalink)
ekkehard.horner
Guest


 
 

Re: regular expressions to replace but keep character?

Nick B schrieb:
Quote:

> Is there a way to use Replace with Regular Expressions but keep a character
> in the original string? A simplified example: If we have "< a >" or "<b >"
> or "< br>" within a string and we want to remove the spaces, but keep the
> character, is there a way this can be done? A Regular Expression can find
> the substring with something like "<\s*[a-z]\s*>" (only finds one character),
> but the ReplaceWith argument appears to be static. Is there any way to keep
> the character?
[...]
You can use $<n> (<n> = number of () in pattern starting with 1) in the
replacement string:

oRE.Pattern = "(<)(\s*)(\S+)(\s*)(>)"
1 2 3 4 5
sRes = oRE.Replace( "< br >", "$1$3$5" )
My System SpecsSystem Spec
Old 07-15-2008   #3 (permalink)
Nick B
Guest


 
 

Re: regular expressions to replace but keep character?

That's exactly what I couldn't find. Thanks!!

"ekkehard.horner" wrote:
Quote:

> Nick B schrieb:
Quote:

> > Is there a way to use Replace with Regular Expressions but keep a character
> > in the original string? A simplified example: If we have "< a >" or "<b >"
> > or "< br>" within a string and we want to remove the spaces, but keep the
> > character, is there a way this can be done? A Regular Expression can find
> > the substring with something like "<\s*[a-z]\s*>" (only finds one character),
> > but the ReplaceWith argument appears to be static. Is there any way to keep
> > the character?
> [...]
> You can use $<n> (<n> = number of () in pattern starting with 1) in the
> replacement string:
>
> oRE.Pattern = "(<)(\s*)(\S+)(\s*)(>)"
> 1 2 3 4 5
> sRes = oRE.Replace( "< br >", "$1$3$5" )
>
My System SpecsSystem Spec
Old 07-15-2008   #4 (permalink)
Old Pedant
Guest


 
 

Re: regular expressions to replace but keep character?

"ekkehard.horner" wrote:
Quote:

>
> oRE.Pattern = "(<)(\s*)(\S+)(\s*)(>)"
> 1 2 3 4 5
> sRes = oRE.Replace( "< br >", "$1$3$5" )
>
You can do it a lot simpler than that.

oRE.Pattern = "<\s*([^\s>]+)\s*>"
sRes = oRE.Replace( "< br >", "<$1>" )

Since you *KNOW* that you are looking for <...> and you *KNOW* you want to
retain <...>, just do *NOT* put any of that stuff in the parentheses. So now
only the non-space, non-> characters will be in parens and there's only one
"group" to be plunked into the output.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
regular expressions - why are they so hard to compose and decipher? PowerShell
$Variables into Regular expressions ?! help PowerShell
New lines in regular expressions PowerShell
How to work with regular expressions PowerShell
Regular expressions PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46