![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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" ) > 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 Specs![]() |
![]() |
| 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 | |||