Results 1 to 7 of 7

Thread: [RESOLVED] Regex problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Resolved [RESOLVED] Regex problem

    I guess this is more of a regex in general problem then vb.net...anyways.

    I am trying to think of a regex replace to replace double quotes with nothing. Example:

    hello("hi there") would become hello(hi there)
    "hi" would become hi
    "example \"3" would become example \"3

    -edit
    Maybe an easier way to explain this is, replace all " that do not have a backslash before them.

  2. #2
    New Member
    Join Date
    Sep 2004
    Posts
    15

    Re: Regex problem

    Can't you use the string.replace function?
    -Phil


    Microsoft Certified Application Developer
    Microsoft Certified Professional
    COMPTia Network+

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Regex problem

    Yeah if you need to just remove all " then try something like this:

    vb Code:
    1. SomeString.Replace("""",String.Empty)

    EDIT: Ah just seen your edit that mentions the \ character, give me a minute and I will post something that does what you are trying to do
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: Regex problem

    try this:

    vb Code:
    1. SomeString.Replace("""",String.Empty).replace("\", "\"")

    its complicated using regex. "\" is used as an escape character. there probably is a way to do it but i don't know how.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Regex problem

    Dammit Paul, I just spent ages putting together this cack handed mess and you go and do it in one line (although your code does assume that EVERY \ character should have a quote mark after it, which may not be the case)

    Here is my miserable attempt, which seems to work but is a bit messy (I suck at using Regex so thought I would just use IndexOf and String.Remove ):
    vb Code:
    1. 'Step 1 - copy the original string
    2. Dim newstring As String = originalstring
    3. Dim counter As Integer = 0
    4.  
    5. 'Step 2 - Keep looping through this code until there are no more quote marks found
    6.         While counter > -1
    7.             counter = newstring.IndexOf("""", counter)
    8.             If counter = 0 Then
    9.                 newstring = newstring.Remove(counter, 1)
    10.             ElseIf Not counter = -1 AndAlso Not newstring(counter - 1) = "\" Then
    11.                 newstring = newstring.Remove(counter, 1)
    12.             ElseIf Not counter = -1 AndAlso newstring(counter - 1) = "\" Then
    13.                 counter += 1
    14.             End If
    15.         End While
    16.  
    17. 'Step 3 - Show the result
    18. MessageBox.Show(newstring)
    This assumes that the variable originalstring contains your string that has the quote marks etc in
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: Regex problem

    sorry Chris. yeah i did assume each \ should have a quotation mark after it.

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Regex problem

    This is how you can do it with regex:

    vb.net Code:
    1. MessageBox.Show(Regex.Replace("""exa""mp""le"" \\""""3""", "(?<=[^\\]|^)""", String.Empty))

    @paul, you escape an escape character by doubling it. IE: \\ matches \

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