Reputation: 1409
I'm working with regex's using the RegEx.replace method and I'm running into some strings my patterns are not working for.
For strings such as:
"Av.Av. Italy"
"Av. Av. Italy"
"Av. . Av. Italy"
I'm trying to replace the Av's and remove all the extra periods and whitespaces so I tyr to use this regex
rgx = new Regex(@"(Av\.).(Av\.)");
address = rgx.Replace(address, replacement);
[edit] I want all of the above strings to end up just saying
"Av. Italy"
But it doesn't change anything.
I also wanted to use a regex to get random periods that appear on some strings (eg: "word . other word") with
rgx= new Regex(@"\b\.\b");
But that doesn't do anything either...
Am I using the escape sequences wrong?
Upvotes: 0
Views: 108
Reputation: 71538
You can perhaps use this regex:
rgx = new Regex(@".*(?:(Av\.)\s*)+");
address = rgx.Replace(address, replacement);
The regex takes any characters, where there's an Av.
somewhere ahead, eats all the duplicate Av.
and spaces and replaces those with a single Av.
(plus a space that got eaten by the regex).
For the second one, maybe that?
rgx= new Regex(@" \.(?= )");
\b
matches between two word characters, namely between \w
and \W
no matter in what order they come, and \w
is [a-zA-Z0-9_]
while \W
is the opposite. Since both space and .
are in \W
, you wouldn't have a match. Then, I used a space instead of \s
because \s
matches newlines, which I don't think is what you're looking for :)
The lookahead is to prevent the removal of two white spaces. Otherwise Word . Word
would become WordWord
.
Upvotes: 1
Reputation: 4530
For the first this rgx = new Regex(@"(Av\.*[\s]*)*");
will work for you. For the second you must provide an example.
Upvotes: 1