Results 1 to 9 of 9

Thread: [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    470

    Resolved [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

    Hello,

    I want to match a string embedded in double quotation marks.
    For ex.

    Code:
    He is "Best Friend" of my family.
    I am going to get
    Code:
    Best Friend
    Could you look at my pattern?

    Code:
    (?<q>["']).*?\k<q>
    Right or not?

    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,453

    Re: [2005] Regular Expression Pattern for double quotation mark

    vb Code:
    1. Dim rx As New Regex("(?<="").+(?="")")

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    470

    Re: [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

    Yes.
    It works out. So how to grab the context within the quotation?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,453

    Re: [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

    vb Code:
    1. Dim testStr As String = "He is ""Best Friend"" of my family."
    2. Dim rx As New Regex("(?<="").+(?="")")
    3. MsgBox(rx.Match(testStr).Value)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    470

    Re: [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

    It doesn't work for some cases.
    For ex.
    Code:
    Dim rx As New Regex("(?<="").+(?="")")
    Expected should be
    Code:
    (?<=
    Code:
    ).+(?=
    Code:
    )
    But use your pattern, I get
    Code:
    (?<="").+(?="")

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,453

    Re: [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

    the regex is made up of 3 parts:

    look behind (?<=stringtofind)

    actual characters to find .+ (= 1 or more of any character)

    look ahead (?=stringtofind)


    if it doesn't work post your text that contains the string you're trying to extract.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    470

    Re: [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

    Code:
    Dim pattern As String
            pattern = "(?<q>["]).*?\k<q> "
    I am using this pattern but I am not sure how to handle the double quote in vb.net.
    Get an syntax error.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,453

    Re: [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

    i don't know. i've never seen regex used that way before. the regex i posted returns the text between the quotes.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,453

    Re: [RESOLVED] [2005] Regular Expression Pattern for double quotation mark

    ok heres how you do it:

    "(?<q>[""]).*?\k<q>"

    will return:

    "Best Friend"

    whereas:

    "(?<="").+?(?="")"

    will return:

    Best Friend

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width