Hello, I have to define a reg expression and I read one way is to use verbatim keyword to avoid to use backslash for escape caracter (I think it's more thin). But maybe It's not so better. How do use it? I have to define something line [^abc\n"]* and I'm confused now:
Regex regexpr = new Regex(@"[^abc\n"]*");
is it correct? How can I insert " inside verbatim string? It gets error because it expects the end of string......
thanks.
Loading
MarcoPosted Sep 22, 2007, 6:31 PM
(([\w]+/?)|/)*(hello\(\))? | (([\w]+/?)|/)*
MarcoPosted Sep 21, 2007, 8:20 PM
string inputString = "a/a/b/hello()"
reg = (([\w]+/?)|/)*(hello\(\))?
"a/a/b/hello" is an OK string....but it must recognize also "a/a/b/hello()" (in one hit) and "a/a/b/"
How can I solve this?
AlanPosted Sep 21, 2007, 5:12 PM
If you have this code:
string inputString = @"hello hello()";[a-z]+) (?hello\(\))";
string expression = @"(?
Regex r = new Regex(expression);
Match matched = r.Match(inputString);
foreach (Capture cap in matched.Groups["first"].Captures)
{
Console.WriteLine("first {0}", cap.ToString());
}
foreach (Capture cap in matched.Groups["second"].Captures)
{
Console.WriteLine("second {0}", cap.ToString());
}
then the output will be:
first hello
second hello()
because there's been an overall match and both groups have therefore captured some text.
Changing the first line to:
string inputString = @"hello hello hello()";
produces the same output as there's still only one overall match.
However, if you change the first line to:
string inputString = @"hello()";
there will be no overall match, the two groups will therefore capture nothing and so there will be nothing output to the console.
MarcoPosted Sep 21, 2007, 12:52 PM
Captures.second show me that it don't captures nothing
foreach (Capture cap in matched.Groups["second"].Captures)
{
Console.WriteLine("second{0}", cap.ToString());
}
But
hello (without ( ) )
AlanPosted Sep 21, 2007, 12:40 PM
Well that in fact is the case.
The first group recognizes any sequence of lower case letters (as long as there's at least one) and, hence, all those sequences that you've given but not hello().
The second group recognizes hello() and nothing else. The only other point is that there must be a space between the two groups.
So, I still don't understand what you're trying to do here.
Perhaps it would help if you gave me a string or strings that the RE doesn't currently recognize but you would like it to recognize.
MarcoPosted Sep 21, 2007, 11:14 AM
@"(?
I just want recognize hello() in
ffddfhello
ffffhello
helloWorld
hello
helloffffff
but not
hello()
that it must recognize by
AlanPosted Sep 21, 2007, 10:56 AM
I'm not clear what you're trying to do here as the regular expression you've given doesn't match any of those combinations. Also ! is not a special character.
To match 'helloworld or 'worldhello' and nothing else all you need is:
string expression = @"^(helloworld|worldhello)$";
Are you perhaps saying that you want to match these combinations regardless of whether they contain upper case or lower case characters?
MarcoPosted Sep 21, 2007, 9:53 AM
(!(\bhello\(\)\b)[a-z]+)
I have this group: the stringInput can be:
helloworld
worldhello
but not hello()world
But my regular doens't accept helloWord. How do this?
AlanPosted Sep 20, 2007, 8:54 AM
Whenever you use a double quote character inside a verbatim string, you have to double it so the compiler doesn't think the end of the string has been reached. So I'd try:
Regex regexpr = new Regex(@"[^abc\n""]*");