Note the false positive ... you may wish to avoid false positives by adding some extra code.
g.
Edit: see http://forums.asp.net/post/5257467.aspx .... imho, eliminating the false positives is easier without resorting to regular expressions ... so far, i've not been able to code a
regular expression that solves the false positive problem ... i'm suspect it can be done with regular expressions, however, it's not going to result in a
simple regex which is what O.P.
damon2012 was hoping to achieve. end Edit.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Edit: here is the output from the above permalink:
>>>--- double false positive
123>>4 good
12**34 good
1--234 good
1*2-3> not found (therefore good) *** false positive
end Edit.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
if you need to use a regular expression to solve a problem, you have two problems.
I full heartedly disagree with this statement.
I would agree regex is often used where it should not be which may be the case here but to damn regex as a whole makes no sense at all... For example complex validation would be one place where regex would excel over most other solutions.
I agree IndexOf works and is more efficient and programatically is the way to go in this case but regex was asked for. I will however withdraw my answer if the original poster were to switch to the use of IndexOf as that would be the better answer in this
case. I was just supplying what was asked for :)
Also I do not see a false positive in your link.
The question was:
I am simply trying to check if a string contains the either of the following character patterns:
Joe, AFAIK, Paul Linton's comment is not meant to damn regular expressions 100%, rather, his comment states a simple truth that regular expressions are likely to cause grief ... a clue to the
complexity of regular expressions is Jeffrey E. F. Friedl's "Mastering Regular Expressions", 3rd edition, which exceeds 500 pages (http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124).
Joe, if i'm correct in my assumption that O.P.
damon2012 is looking for ">>", but not for ">>>", et cetera ... then your suggested answer needs to be revised to eliminate false positives.
rgds/gerry
@ damon2012 :: perhaps you could clarify whether more than two consectutive of "-", "*", or ">" are valid for
your needs?
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
for your needs, are the following legal or illegal?:
--- *** >>>
if they could occur and also if they are illegal, then you'll need either a more complete (and
likely more complicated) regular expression or you can relatively
easily modify the non regular expression thus:
Match: [A: >>:]
Match: [B: --]
Not a Match: [C: *** false positive]
No Match: [D: negative]
Match: [E: ** okay]
Not a Match: [F: ---]
Not a Match: [G: >>>]
Match: [>>]
Match: [**]
Match: [--]
my example interprets the following as unmatched:
Not a Match: [>> >>>]
Not a Match: [** ***]
Not a Match: [-- ---]
Not a Match: [-- >>>]
Not a Match: [>> ***]
Not a Match: [** ---]
one could decide to instead interpret the above as matched; interpretation depends upon one's needs.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
damon2012
Member
21 Points
106 Posts
Help simple regex
Jan 02, 2013 11:08 AM|LINK
Hi,
I am simply trying to check if a string contains the either of the following character patterns:
>>
**
--
I have been trying this in several ways but it is not working correctly. "[>>][**][--]". What is the correct way to do this please?
Thanks.
sameer_khanj...
Contributor
7056 Points
1376 Posts
Re: Help simple regex
Jan 02, 2013 11:19 AM|LINK
EDITED
I think you can try
[\**\>>\--]
sameer.khanjit@gmail.com
View Blog
Click "Mark as Answer" on the post that helped you.
NadeemZee
Participant
942 Points
178 Posts
Re: Help simple regex
Jan 02, 2013 11:19 AM|LINK
hi,
check the following links:
http://www.regular-expressions.info/reference.html
http://msdn.microsoft.com/en-us/library/844skk0h.aspx
Do FEAR (Face Everything And Rise)
Please mark as Answer if my post helps you..!
jprochazka
Contributor
4822 Points
733 Posts
Re: Help simple regex
Jan 02, 2013 11:42 AM|LINK
Give this a try:
Tester link:
http://regexstorm.net/tester?p=%5b*%5d%5b*%5d%7c%5b%3e%5d%5b%3e%5d%7c%5b-%5d%5b-%5d&i=123%3e%3e4%0d%0a12**34%0d%0a1--234%0d%0a1*2-3%3e&o=m
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Help simple regex
Jan 02, 2013 05:00 PM|LINK
@ damon2012
paraphrasing Paul Linton, if you need to use a regular expression to solve a problem, you have two problems.
you can also solve this problem with the String.IndexOf method:
List<System.String> testStrings = new List<System.String>() { "A: >>:", "B: --", "C: *** false positive", "D: negative", "E: ** okay" }; foreach (System.String testString in testStrings) { if((testString.IndexOf(">>") >= 0) || (testString.IndexOf("**") >= 0) || (testString.IndexOf("--") >= 0)) Console.WriteLine("Match: [{0}]", testString); else Console.WriteLine("No Match: [{0}]", testString); }output:
Note the false positive ... you may wish to avoid false positives by adding some extra code.
g.
Edit: see http://forums.asp.net/post/5257467.aspx .... imho, eliminating the false positives is easier without resorting to regular expressions ... so far, i've not been able to code a regular expression that solves the false positive problem ... i'm suspect it can be done with regular expressions, however, it's not going to result in a simple regex which is what O.P. damon2012 was hoping to achieve. end Edit.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Help simple regex
Jan 02, 2013 05:07 PM|LINK
@ jprochazka
i like the regexstorm.net tester ... like my example with .IndexOf, the regexstorm.net tester also gives false positives as per this permalink: http://regexstorm.net/tester?p=%5b*%5d%5b*%5d%7c%5b%3e%5d%5b%3e%5d%7c%5b-%5d%5b-%5d&i=%3e%3e%3e---%0d%0a123%3e%3e4%0d%0a12**34%0d%0a1--234%0d%0a1*2-3%3e%0d%0a***&o=m
rgds/gerry
Edit: here is the output from the above permalink:
>>>--- double false positive
123>>4 good
12**34 good
1--234 good
1*2-3> not found (therefore good)
*** false positive
end Edit.
jprochazka
Contributor
4822 Points
733 Posts
Re: Help simple regex
Jan 02, 2013 05:31 PM|LINK
I full heartedly disagree with this statement.
I would agree regex is often used where it should not be which may be the case here but to damn regex as a whole makes no sense at all... For example complex validation would be one place where regex would excel over most other solutions.
I agree IndexOf works and is more efficient and programatically is the way to go in this case but regex was asked for. I will however withdraw my answer if the original poster were to switch to the use of IndexOf as that would be the better answer in this case. I was just supplying what was asked for :)
Also I do not see a false positive in your link.
The question was:
*** contains ** which is not a false positive.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Help simple regex
Jan 02, 2013 05:54 PM|LINK
@ jprochazka
see Help with sorting based on several criteria wherein O.P. damon2012 is dealing with strings like:
3 Item >> 3.1 sub item >> 3.1.1 sub sub item
Joe, AFAIK, Paul Linton's comment is not meant to damn regular expressions 100%, rather, his comment states a simple truth that regular expressions are likely to cause grief ... a clue to the complexity of regular expressions is Jeffrey E. F. Friedl's "Mastering Regular Expressions", 3rd edition, which exceeds 500 pages (http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124).
Joe, if i'm correct in my assumption that O.P. damon2012 is looking for ">>", but not for ">>>", et cetera ... then your suggested answer needs to be revised to eliminate false positives.
rgds/gerry
@ damon2012 :: perhaps you could clarify whether more than two consectutive of "-", "*", or ">" are valid for your needs?
damon2012
Member
21 Points
106 Posts
Re: Help simple regex
Jan 02, 2013 06:01 PM|LINK
Hi,
at the moment, the special characters are only occurring in doubles. There are no single ones to deal with,
thanks.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Help simple regex
Jan 02, 2013 08:33 PM|LINK
@ damon2012
for your needs, are the following legal or illegal?:
---
***
>>>
if they could occur and also if they are illegal, then you'll need either a more complete (and likely more complicated) regular expression or you can relatively easily modify the non regular expression thus:
List<System.String> testStrings = new List<System.String>() { "A: >>:", "B: --", "C: *** false positive", "D: negative", "E: ** okay", "F: ---", "G: >>>", ">>", "**", "--", ">> >>>", "** ***", "-- ---", "-- >>>", ">> ***", "** ---" }; foreach (System.String testString in testStrings) { Int32 indexValueGT2 = testString.IndexOf(">>"); Int32 indexValueGT3 = testString.IndexOf(">>>"); Int32 indexValueAsterisk2 = testString.IndexOf("**"); Int32 indexValueAsterisk3 = testString.IndexOf("***"); Int32 indexValueMinus2 = testString.IndexOf("--"); Int32 indexValueMinus3 = testString.IndexOf("---"); if((indexValueGT2 >= 0) || (indexValueAsterisk2 >= 0) || (indexValueMinus2 >= 0)) { Boolean okayString = true; if((indexValueGT2 >= 0) && (indexValueGT3 >= 0)) okayString = false; if((indexValueAsterisk2 >= 0) && (indexValueAsterisk3 >= 0)) okayString = false; if((indexValueMinus2 >= 0) && (indexValueMinus3 >= 0)) okayString = false; if(okayString) Console.WriteLine("Match: [{0}]", testString); else Console.WriteLine("Not a Match: [{0}]", testString); } else Console.WriteLine("No Match: [{0}]", testString); }output:
my example interprets the following as unmatched:
one could decide to instead interpret the above as matched; interpretation depends upon one's needs.
g.