locked
How do I match and not capture something in brackets in the middle of a string that I am capturing?

    Question

  • This is my input string:

    , a. [the Cat.] Pertaining to the Cat.

     

    This is my regular expression:

    (?!^|,)([\s,\S]*?)[A-Z]

    (?!^|,) means match the beginning of the string or a comma, negative lookahead

    ([\s,\S]*?) means match anything

    [A-Z] means match a capital letter-- in this case, it was meant to capture 'P' from "Pertaining to..."

     

    This is what I catch:

    a. [the

     

    This is what I want to catch:

    a.

     

    Some input strings, like the one above, have [some text in brackets] while some input strings do NOT.

    If there is bracketed text in the string with a capital letter,

    1) how do I prevent the capital letter in the brackets from matching the [A-Z], and

    2) how do I not capture the bracketed text?

     

    Thanks for the help!



    Friday, March 25, 2011 3:05 PM

Answers

  • Hi,

    don't know if (?!^|,) is needed in that case.

    1) extend [A-Z] to (?:\[[A-Z][^]]*\]|[A-Z])

    2) To capture, you can use (...) without option RegexOptions.ExplicitCapture (?<slotname>...). To not capture, you can use RegexOptions.ExplicitCapture and (...) or (?:...)

    Hope I did understand what you want...

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Marked as answer by Paul Zhou Monday, April 04, 2011 2:10 AM
    Friday, March 25, 2011 8:23 PM

All replies

  • So,

    You want to capture all text in the beginning of the string before a Capital letter with or with a bracket?

    How about :

     


    Ctrl+Z

     

    ^[^A-Z\[]+

    It says...gimme everything in front of a capital letter or [

    Adam

    Friday, March 25, 2011 3:27 PM
  • No, I want to capture up until a capital letter but ignore any capital letter that is in brackets.
    Friday, March 25, 2011 6:24 PM
  • Can you provide a string of a sample and then tell us what you want to capture because your example above is a little ambiguous. Don't tell us your patterns but what you want to capture.

    John Grove, MCC - Senior Software Engineer
    Friday, March 25, 2011 6:26 PM
  • Try this:

    ^[^A-Z\[?]+


    Ctrl+Z
    Friday, March 25, 2011 7:38 PM
  • The questioner says:

    Here is my string: a. [the Cat.] Pertaining to the Cat.

    This is what I want to catch: --> a.

     

    then just do this --> a\.


    John Grove, MCC - Senior Software Engineer
    Friday, March 25, 2011 7:58 PM
  • Hi,

    don't know if (?!^|,) is needed in that case.

    1) extend [A-Z] to (?:\[[A-Z][^]]*\]|[A-Z])

    2) To capture, you can use (...) without option RegexOptions.ExplicitCapture (?<slotname>...). To not capture, you can use RegexOptions.ExplicitCapture and (...) or (?:...)

    Hope I did understand what you want...

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Marked as answer by Paul Zhou Monday, April 04, 2011 2:10 AM
    Friday, March 25, 2011 8:23 PM
  • Hi mbwitting,
    Has your issue been resolved?

    Now I will mark an answer. You could “Unmark As Answer” if it doesn’t make sense.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 04, 2011 2:10 AM