16

I have a Resoure.resx file that I need to search to find strings ending with a whitespace. I have noticed that in Visual Web Developer I can search using both regex and wildcards but I can not figure out how to find only strings with whitespace in the end. I tried this regex but didn't work:

\s$

Can you give me an example? Thanks!

Martin
  • 219
  • 1
  • 5
  • 10
  • Can you show your code? That regex should be matching any string that has at least one whitespace character **at** the end of the string, which from your question sounds like what you're looking for. – T.J. Crowder Dec 15 '10 at 09:21
  • Yep - it what sense did it not work? – Dogweather Dec 15 '10 at 09:38
  • I have no code, just using "Find and Replace" feature in Visual Web Developer, and I choosed "Regular Expressions". Is it not possible to search in resx-files? – Martin Dec 15 '10 at 09:55
  • Ah, okay. FWIW, I've answered on the basis of that, then. – T.J. Crowder Dec 15 '10 at 10:17

3 Answers3

24

I'd expect that to work, although since \s includes \n and \r, perhaps it's getting confused. Or I suppose it's possible (but really unlikely) that the flavor of regular expressions that Visual Web Developer uses (I don't have a copy) doesn't have the \s character class. Try this:

[ \f\t\v]$

...which searches for a space, formfeed, tab, or vertical tab at the end of a line.

If you're doing a search and replace and want to get rid of all of the whitespace at the end of the line, then as RageZ points out, you'll want to include a greedy quantifier (+ meaning "one or more") so that you grab as much as you can:

[ \f\t\v]+$
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks a lot! It seems strange though, it searches and find strings but none of them ends with a whitespace. Maybe I should open the file in another program that handles regex and search there. – Martin Dec 15 '10 at 10:24
8

You were almost there. adding the + sign means 1 characters to infinite number of characters.

This would probably make it:

\s+$
RageZ
  • 26,800
  • 12
  • 67
  • 76
  • If his goal is just to find strings that have *any* whitespace at the end, there's no need for the `+`; just matching the last whitespace character is sufficient. – T.J. Crowder Dec 15 '10 at 09:20
  • 2
    @TJ: yes but I think the op wants to get rid off any whitespace in the end of the line. I suppose – RageZ Dec 15 '10 at 09:21
2

Perhaps this would work:

^.+\s$

Using this you'll be able to find nonempty lines that end with a whitespace character.

darioo
  • 46,442
  • 10
  • 75
  • 103
  • If a line ends with a whitespace character, it is by definition non-empty. No need for the `^.+`. – T.J. Crowder Dec 15 '10 at 10:09
  • @T.J.: true; but I assumed OP wanted to find lines with some content and whitespace at the end (although `^.+` might as well match only whitespace at line beginning) – darioo Dec 15 '10 at 10:17