locked
Match Space but only inside word characters RRS feed

  • Question

  • I want to match on \s inside a string but not match if the \s occurs before the string.

     

     

    Tuesday, May 11, 2010 12:37 PM

Answers

  • Hi,

    I agree with John...

    Patter (example): ^\s*(?<words>\w.+\w)\s*$

    Options: Multiline

    It returns a match for each line. In the named group "words" there are all words matched. At the beginning and at the end of the line there could be whitespace characters...

    Multiple blanks between two words are matched by this pattern, too.

    Greetings

     


    Wolfgang Kluge
    gehirnwindung.de
    • Proposed as answer by JohnGrove Tuesday, May 11, 2010 8:16 PM
    • Marked as answer by Mattaniah Wednesday, May 12, 2010 11:17 AM
    Tuesday, May 11, 2010 7:36 PM
  • ignore leading spaces by not capturing them, or if you must capture them then encapsulate your "real" match in a group.

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Mattaniah Wednesday, May 12, 2010 11:17 AM
    Tuesday, May 11, 2010 6:56 PM

All replies

  • Can you provide examples of what you want to match vs what you don't want to match?

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, May 11, 2010 3:13 PM
  • I want to capture "not on my life" in this string: "        not on my life"

    Tuesday, May 11, 2010 4:05 PM
  • exactly this? I am guessing that your sentence does not start like "not on my life". 

    I am also guessing it is not exactly this:

    String pattern = @"not\son\smy\slife";


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, May 11, 2010 4:35 PM
  • No, it is an example of ignoring leading spaces but capturing spaces in between.
    Tuesday, May 11, 2010 6:47 PM
  • ignore leading spaces by not capturing them, or if you must capture them then encapsulate your "real" match in a group.

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Mattaniah Wednesday, May 12, 2010 11:17 AM
    Tuesday, May 11, 2010 6:56 PM
  • Hi,

    I agree with John...

    Patter (example): ^\s*(?<words>\w.+\w)\s*$

    Options: Multiline

    It returns a match for each line. In the named group "words" there are all words matched. At the beginning and at the end of the line there could be whitespace characters...

    Multiple blanks between two words are matched by this pattern, too.

    Greetings

     


    Wolfgang Kluge
    gehirnwindung.de
    • Proposed as answer by JohnGrove Tuesday, May 11, 2010 8:16 PM
    • Marked as answer by Mattaniah Wednesday, May 12, 2010 11:17 AM
    Tuesday, May 11, 2010 7:36 PM