4

i got a little trouble using Rexex in Powershell. It seems like there is a imlementation error or something.

The text i want to work with is a html file, which looks like this (Example1):

<span>[Mobile: %mobile% |] Phone: %telephone% [| Fax: %faxNumber%]</span>
<Span>

The Problem is that, caused by html editors, i also may get something like this (Example2):

<span>[Mobile: 

%mobile% |] Phone: %telephone% [| Fax: &nbsp;&nbsp;%faxNumber%]</span>

So as you see, we got linebreaks and html escaped, fixed whitespaces &nbsp;.

My Powershell Regex looks like this:

$x = $x -ireplace '(?ms)\[(.?){7}Fax(.*?)\]', 'MyReplacement1'

and this

$x = $x -ireplace '(?ms)\[(.?){7}Mobile(.*?)\]', 'MyReplacement2'

Basicly The [ marks the beginning of a variable and ] the end of it. Two problems arise from this:

  1. Since we got two variables, mobile and fax, i'm using (.?){7} to allow SOME (here exacly 7) characters and avoid matching the hole part between the first [ near Mobile and the last ] near Fax (which would happen if i would be using (.*?) instead of (.?){7}). I'm not sure if there are alternatives so that i can allow ANY number (and not 7) of chars between the starting [ and the variable keyword "Fax" for example. This would be usefull to avoid missmatches when stuff like &nbsp;&nbsp; gets added (where only 7 char would not be enough and like i said (.*?) will fail). Hope i was able to explain it (kinda hard) - if not: please feel free to ask!
  2. Powershells -replace method dosn't offer a way to set regex options, therefore i got to use (?ms) to set DotAll and multiline modes. As you see, I'm using it within my regex pattern. However: when a newline is added, as you see in example2 between the words Mobile: and %mobile%, the regex fails and nothing gets replaced!

I'm greatfull for any help and even regex recommandations from the pros to avoid any further problems i'm not thinking about right now...

EDIT: (Example3):

<span>[Mobile: 

%mobile% |] Phone: %telephone% [| Fax: 
%faxNumber%]</span>
1
  • Yeah, I already saw that. See my comment on the answer, and let's delete our comments here ;) Dec 2, 2012 at 22:26

1 Answer 1

11

The trick around DotAll mode is to use [\s\S] instead of .. This character class matches any character (because it matches space and non-space characters). (As does [\w\W] or [\d\D], but the spaces seem to be kind of a convention.)

To get around the 7 you can simply disallow closing ] before the one you actually want to match (that by the way also makes DotAll unnecessary). So something like this should work fine for you:

\[([^\]:]*)Fax([^\]]*)\]

It looks a bit ugly, but it simply means this:

\[        # literal [
(         # capturing group 1
  [^\]:]* # match as many non-:, non-] characters as possible
)         # end of group 1
Fax       # literal Fax
(         # capturing group 2
  [^\]]*  # match as many non-] characters as possible
)         # end of group 2
\]        # literal ]

Further reading on character classes.

Note that none of these patterns need multiline mode m (neither yours nor mine), because all it does is make ^ and $ match line beginnings and endings, respectively. But none of the patterns contain these meta-characters. So the modifier does not do anything.

My console output:

PS> $x = "<span>[Mobile: %mobile% |] Phone: %telephone% [| Fax: &nbsp;&nbsp;%faxNumber%]</span>"
PS> $x -ireplace '\[([^\]:]*)Mobile([^\]]*)\]', 'MyReplacement1'
<span>MyReplacement1 Phone: %telephone% [| Fax: &nbsp;&nbsp;%faxNumber%]</span>
PS> $x -ireplace '\[([^\]:]*)Fax([^\]]*)\]', 'MyReplacement2'
<span>[Mobile: %mobile% |] Phone: %telephone% MyReplacement2</span>
6
  • Thanks for your great answer. Some questions remain though: what is the non-: match for? Also, when running that pattern for my two Powershell commands, mobile gets replaced just fine, but [| Fax: %faxNumber%] is not replaced :-(. Running a online Rexex validator, your Regex seems to be correct thought.
    – omni
    Dec 2, 2012 at 22:00
  • @masi I added the : to avoid a match of a potential some[thing: %Fax%]. But if it is not relevant to you where the Fax occurs between the brackets, you can remove the colon from the character class. I will check again that my answer works with the latter example. Dec 2, 2012 at 22:02
  • @masi hm, it works perfectly fine for me. (however, I changed around the square bracket in the first character class, but with your input string, it shouldn't make a difference; anyway, see my edit) Dec 2, 2012 at 22:05
  • Instead of assigning the $x with a in-line string, when you import the string like this $mystring = Get-Content -Path .\test.html where test.html contains the content i just added to my posting (Example3), then it dosn't work. It seems to be a problem with the linebreak
    – omni
    Dec 2, 2012 at 22:18
  • @masi this answer seems to have the reason for the issue. Get-Content doesn't result in a string with line breaks, but instead with a string for each line, which needs to be joined back together, so -ireplace can work on the whole file. Dec 2, 2012 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.