|
-
Jul 13th, 2009, 01:38 AM
#1
Thread Starter
Lively Member
[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.
-
Jul 13th, 2009, 07:46 AM
#2
New Member
Re: Regex problem
Can't you use the string.replace function?
-Phil
Microsoft Certified Application Developer
Microsoft Certified Professional
COMPTia Network+
-
Jul 13th, 2009, 08:03 AM
#3
Re: Regex problem
Yeah if you need to just remove all " then try something like this:
vb Code:
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
-
Jul 13th, 2009, 08:20 AM
#4
Re: Regex problem
try this:
vb Code:
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 13th, 2009, 08:29 AM
#5
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:
'Step 1 - copy the original string
Dim newstring As String = originalstring
Dim counter As Integer = 0
'Step 2 - Keep looping through this code until there are no more quote marks found
While counter > -1
counter = newstring.IndexOf("""", counter)
If counter = 0 Then
newstring = newstring.Remove(counter, 1)
ElseIf Not counter = -1 AndAlso Not newstring(counter - 1) = "\" Then
newstring = newstring.Remove(counter, 1)
ElseIf Not counter = -1 AndAlso newstring(counter - 1) = "\" Then
counter += 1
End If
End While
'Step 3 - Show the result
MessageBox.Show(newstring)
This assumes that the variable originalstring contains your string that has the quote marks etc in
-
Jul 13th, 2009, 08:34 AM
#6
Re: Regex problem
sorry Chris. yeah i did assume each \ should have a quotation mark after it.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 13th, 2009, 10:36 AM
#7
Re: Regex problem
This is how you can do it with regex:
vb.net Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|