regex question

  • Thread starter Thread starter bonit99
  • Start date Start date
B

bonit99

hello!

I have a question on how I can replace all occurences of one special
character within a matching string?

for example string:

key = 'som#ething#something' OR anotherkey <= 'anot#her' AND somekey =
'some#'

would have to replace the character # within any string between the
two quotas ' ' with some other character...

this was my approach for also matching the key, but it would only find
it and not replace it + it would only find one occurence. but now I am
stuck

(.*\s[=><*%]{1,2}\s'(?<value>.*)')

any ideas?

thanks a lot

Hans
 
Sorry, Hans. I read that over quite a few times and I'm still not exactly
sure what you're after. It's probably me that's terminally thick, but just
in case other people are having the same trouble, would you mind having
another go. Perhaps give an example of what the string looks like going in,
and what you want it look like going out.

Once again, my apologies if it's just me being stupid. It wouldn't be the
first time.


Peter
 
Hello Peter!

thanks for your reply, probably I have not written the things
properly. Sorry for my english.
ok lemme try again to re-phrase my need,

I have a string for example: (see it as string not as operation
keywords like OR etc, they are just part of the string - it is some
special sql string to modify)

value = 'hello this is#the example' OR value2 <= 'this#one' AND value3
IS NOT 'something#'

within this string you see the character "#" without the quotas
several times. I want to replace any occuring "#" (without quotas)
with character " " (without quotas) (blank character)

the rule for this is the string:

will start with any number of characters or numbers: e.g. value
then it will be followed by: blank [any number of characters] blank
e.g. [blank]IS NOT[blank]
then: character '
then: <the text which shall be searched for # and replaced by a blank>
then character '

this would represent the search criterion, this part shall be searched
as often it occurs inside this string and then apply the replacement


(the example text is just symbolic to show you what I want to achieve,
don't try to interprete the operators or the sense of the string)

hope it's now a little clearer,
thanks in advance

Hans
 
If I understand you correctly, you want to find all occurrences of the '#'
character that are enclosed in a string that is enclosed in single quotes,
within a string. In your examples, there was only ONE occurrence of the '#'
character within any substring, so I wrote the following with that rule in
mind:

(?<='[^'#]*)#(?=[^'#]*')

This is a regular expression that begins with a positive look-behind and
ends with a positive look-ahead. The effect of these is that the match will
be matched, but the '#' character is the only one that is captured. The
first positive look-behind asserts that the '#' character must be preceded
by a single quote character followed by zero or more characters that are not
single quotes or '#' characters. The positive look-ahead indicates that the
'#' character must be followed by zero or more characters that are not a
single quote or a '#' character, followed by a single quote.

In your example (below), there will be 3 matches, one per '#' character:

value = 'hello this is#the example' OR
value2 <= 'this#one' AND value3
IS NOT 'something#'

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Hello Kevin!

your solution is correct, but could you please tell me what to add
that is would
replace any occurance of # within that ' ' string (as you said, your
regex now only accepts one #.)

for example if you have string:

key = 'som##ething#something'

thanks very much

Hans
 
Thanks Hans. And it's quite possibly my English that's to blame, not yours
:)

I can't see an easy way of doing it easily via a RE, but I can see a
procedural way of doing it. Here's some pseudo code:

Convert the string to an array of char
bool InQuotes = false;
StringBuilder sb = new StringBuilder();

foreach char c in the array of char
{
if c is a single quote
InQuotes = !InQuotes;

if InQuotes
if (c is a # character)
c = ' ' // i.e. blank

Add c to the StringBuilder
}

Convert the StringBuilder back to a string

Or something like that. Not as elegant as using an RE...

HTH


Peter
 
The problem here is that you are using the outer single quotes to identify a
match. You might have to split the string by identifying the single quote
pairs and then do a substitution on each substring.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Kevin Spencer said:
If I understand you correctly, you want to find all occurrences of the '#'
character that are enclosed in a string that is enclosed in single quotes,
within a string. In your examples, there was only ONE occurrence of the
'#' character within any substring, so I wrote the following with that
rule in mind:

(?<='[^'#]*)#(?=[^'#]*')

This is a regular expression that begins with a positive look-behind and
ends with a positive look-ahead. The effect of these is that the match
will be matched, but the '#' character is the only one that is captured.
The first positive look-behind asserts that the '#' character must be
preceded by a single quote character followed by zero or more characters
that are not single quotes or '#' characters. The positive look-ahead
indicates that the '#' character must be followed by zero or more
characters that are not a single quote or a '#' character, followed by a
single quote.

In your example (below), there will be 3 matches, one per '#' character:

value = 'hello this is#the example' OR
value2 <= 'this#one' AND value3
IS NOT 'something#'

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Hello Peter!

thanks for your reply, probably I have not written the things
properly. Sorry for my english.
ok lemme try again to re-phrase my need,

I have a string for example: (see it as string not as operation
keywords like OR etc, they are just part of the string - it is some
special sql string to modify)

value = 'hello this is#the example' OR value2 <= 'this#one' AND value3
IS NOT 'something#'

within this string you see the character "#" without the quotas
several times. I want to replace any occuring "#" (without quotas)
with character " " (without quotas) (blank character)

the rule for this is the string:

will start with any number of characters or numbers: e.g. value
then it will be followed by: blank [any number of characters] blank
e.g. [blank]IS NOT[blank]
then: character '
then: <the text which shall be searched for # and replaced by a blank>
then character '

this would represent the search criterion, this part shall be searched
as often it occurs inside this string and then apply the replacement


(the example text is just symbolic to show you what I want to achieve,
don't try to interprete the operators or the sense of the string)

hope it's now a little clearer,
thanks in advance

Hans
 
Hello!

Thanks Peter, I will consider your solution!
Thanks also Kevin, I have understood.

Sincerly

Hans
 
Do you know in advance what the character to be replaced is (or the
range of possibilities for it)?
Would that character ever appear outside the quotes anyway (outside a
value and as part of
the SQL string maybe)? If not, you can simplify by using a
string.remove. (yes, it's obvious, but
I'm posting because I just replaced a bunch of regex code in our
project with string manipulation
code and it's WAY faster).
 

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

Back
Top