Wednesday 15 August 2012

powershell v2.0 - How do I escape Regex to search on a period? -


There is a simple assignment to mess with me and I need some more eyes. I'm sure I miss something easy We have a directory of files that contains all the special characters, and I need to leave only alpha, numeric, dot and underscore character. I am using regex within PowerShell v2.0 script.

For example:

! Foo12.log becomes foo12.log

foo1 (bar) 2.log becomes foo1bar2.log

[foo] bar_.log becomes foobar_.log

Uses and lists of my tactics Think out and replace everything with "" :

  $ bkpPath = "\\ server \ foo" gci $ bkpPath | When I ran it, I ended up with  foo12log     

code>, foo1bar2log and foobar_log < / Code> so I change regex to include the . : [^ a-zA-Z _ \. 0- 9] . It does not remove any special characters, I also have [^ a-zA-Z _ \ [\] \ (\) \. 0- 9] , with the same period when I have avoided a period.

I doubt that with my escape the duration is . and it is reading as a wildcard. If this is happening, how can I fix it? If it is not so what is happening?

Because "." "Anything" means that the use of that particular character will be silly inside the square chambers. So in this case, the full stop loses its meaning and you do not have to use the "escape" character before it.

In addition, it is worth noting that:

\ W means "any word character" (letter, number, underscore)

\ W Means "any non-word character" (Although it is not time-saver in this case, because you want

in this case, your relevant Reigate can be just:

[^ \ w.]

No comments:

Post a Comment