Regex - unable to locate ASCII characters

G

Guest

Hi,

I need to replace double quotes inside the text from the database with " to
correctly display my text. I was trying to use Regex to perform such a task:
Regex.Replace(text, "[\x22\x93\x94]", """)
to catch standard " sign and non-standard “ and †- but non-standard double
quotes are not recognised by Replace function.

Can anyone suggest the correct expression to use (I was trying to avoid
multiple String.Replace functions that work fine - I need to make similar
substitution on a single quote Ascii codes 96, 145 & 146 - so I could only
look through a string twice instead of six times)

Any help is greatly appreciated.
 
G

Guest

Hi,

I need to replace double quotes inside the text from the database with " to
correctly display my text. I was trying to use Regex to perform such a task:
Regex.Replace(text, "[\x22\x93\x94]", """)
to catch standard " sign and non-standard " and " - but non-standard double
quotes are not recognised by Replace function.

Can anyone suggest the correct expression to use (I was trying to avoid
multiple String.Replace functions that work fine - I need to make similar
substitution on a single quote Ascii codes 96, 145 & 146 - so I could only
look through a string twice instead of six times)

Any help is greatly appreciated.

\"|\"
 
G

Guest

Alex,

Thank you - though you cannot escape neither “ nor †in C# - it does not
compile, but if you just list them as [“â€] it seems to be working. I was
actually working in VB where quotes management is a bit tricky - and I had to
work with Ascii codes to actually get the character.

Anyhow, is there a way I could do one string pass instead of multiple ones
(one - for double quotes, one - for single, one - for incorrect dashes, etc),
so that I could say, if any of the double quotes "[\"“â€]" then replace with
""", if any of single quotes [‘’`] then replace with ', if long dash –
replace with normal "-"?


Anon User said:
Hi,

I need to replace double quotes inside the text from the database with " to
correctly display my text. I was trying to use Regex to perform such a task:
Regex.Replace(text, "[\x22\x93\x94]", """)
to catch standard " sign and non-standard " and " - but non-standard double
quotes are not recognised by Replace function.

Can anyone suggest the correct expression to use (I was trying to avoid
multiple String.Replace functions that work fine - I need to make similar
substitution on a single quote Ascii codes 96, 145 & 146 - so I could only
look through a string twice instead of six times)

Any help is greatly appreciated.

\"|\"
 
G

Guest

Alex,

Thank you - though you cannot escape neither " nor " in C# - it does not
compile, but if you just list them as [""] it seems to be working. I was
actually working in VB where quotes management is a bit tricky - and I had to
work with Ascii codes to actually get the character.

Check the encoding of the file, there should be no problem to save {"
"}

File - Save As...

Anyhow, is there a way I could do one string pass instead of multiple ones
(one - for double quotes, one - for single, one - for incorrect dashes, etc),
so that I could say, if any of the double quotes "[\"""]" then replace with
""", if any of single quotes [''`] then replace with ', if long dash -
replace with normal "-"?

something like the following loop

string[] c = new string[] { "\"|\"", "\-" };
string[] r = new string[] { @"\"", "\-" };

for (int i = 0; i < c.Length; i++)
{
txt = Regex.Replace(txt, c, r, RegexOptions.Compiled);
}
 
G

Guest

Thank you - though you cannot escape neither " nor " in C# - it does not
compile, but if you just list them as [""] it seems to be working. I was
actually working in VB where quotes management is a bit tricky - and I had to
work with Ascii codes to actually get the character.

Check the encoding of the file, there should be no problem to save {"
"}

File - Save As...
Anyhow, is there a way I could do one string pass instead of multiple ones
(one - for double quotes, one - for single, one - for incorrect dashes, etc),
so that I could say, if any of the double quotes "[\"""]" then replace with
""", if any of single quotes [''`] then replace with ', if long dash -
replace with normal "-"?

something like the following loop

string[] c = new string[] { "\"|\"", "\-" };
string[] r = new string[] { @"\"", "\-" };

for (int i = 0; i < c.Length; i++)
{
txt = Regex.Replace(txt, c, r, RegexOptions.Compiled);



}- Hide quoted text -

- Show quoted text -


I've noticed that Google replaced all quotes in my posts to the ( " )
 
G

Guest

Alexey,

The solution you propose does two string passes, and I was asking whether it
is possible to do 1 string pass instead - my strings are relatively long
(could be a few thousands of characters), and I need to process quite a
number of them - I was looking for optimising

Anon User said:
Thank you - though you cannot escape neither " nor " in C# - it does not
compile, but if you just list them as [""] it seems to be working. I was
actually working in VB where quotes management is a bit tricky - and I had to
work with Ascii codes to actually get the character.

Check the encoding of the file, there should be no problem to save {"
"}

File - Save As...
Anyhow, is there a way I could do one string pass instead of multiple ones
(one - for double quotes, one - for single, one - for incorrect dashes, etc),
so that I could say, if any of the double quotes "[\"""]" then replace with
""", if any of single quotes [''`] then replace with ', if long dash -
replace with normal "-"?

something like the following loop

string[] c = new string[] { "\"|\"", "\-" };
string[] r = new string[] { @"\"", "\-" };

for (int i = 0; i < c.Length; i++)
{
txt = Regex.Replace(txt, c, r, RegexOptions.Compiled);



}- Hide quoted text -

- Show quoted text -


I've noticed that Google replaced all quotes in my posts to the ( " )
 
G

Guest

Alexey,

The solution you propose does two string passes, and I was asking whether it
is possible to do 1 string pass instead - my strings are relatively long
(could be a few thousands of characters), and I need to process quite a
number of them - I was looking for optimising

You can't multiple replace using a single replace string.

There is another Regex.Replace with a MatchEvaluator delegate to a
custom function that can return a replacement, based on the received
match.

Regex rx = new Regex("\"|\"", "\-" ");
txt = rx.Replace(text, new MatchEvaluator(myreplace));

static string myreplace(Match m)
{
if (m.ToString() == "-")
{
return "-";
} else if ...
return...
}
}

I don't know if it will be faster, I think it's not
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top