by kdn242 at 2013-04-01 08:05:34
Hi,by mjolinor at 2013-04-01 08:18:45
I want to replace any character before the Development with a string ""C]Development[/color]\andworkfile\ect"
How do I do that with a regular expression that can match it ?
Thanks for your time!!!
kdn242
$string = "C:\WorkFile\files\Development_vob\Development\andworkfile\ect"
$regex = '.+\(Development.+)'
$replace = 'This is my replacement string'
if ($string -match $regex)
{$string = $replace + $matches[1]}
$string
This is my replacement string\Development\andworkfile\ect</code>
Lightbulb?
by nohandle at 2013-04-02 03:35:15Personally I’d go with word boundaries (\b) rather than backshlashes in the match.
I tried to use -replace operator to finish the task - the idea was to use positive lookahead to make the regex quite elegant but for some reason it adds the replace string two times although it is made part of the resulting caprture.
"C:\WorkFile\files\Development_vob\Development\andworkfile\ect" -replace '.?(?=\bDevelopment\b)','C:\new_path'
[regex]]
C:\new_path\C:\new_path\Development\andworkfile\ect<br>C:\new_path\C:\new_path\Development\andworkfile\ect</code>
so what i was forced to end up with was:
"C:\WorkFile\files\Development_vob\Development\andworkfile\ect" -replace '.
?(\bDevelopment\b)','C]
C:\new_path\Development\andworkfile\ect</code>
Adding the second capture back to the string using the $1 (indexed from 0)
by mjolinor at 2013-04-02 06:41:55[quote]Personally I’d go with word boundaries (\b) rather than backshlashes in the match. [/quote]
I chose the backslashes because they are the delimiters for file paths, and we’re looking for a specific directory name. ‘\Development\’ can only match a directory named ‘Development’.
‘\bDevelopment\b’ will match a directory named ‘Development’, but it will also match a directory named e.g. ‘Program Development’.