|
i have this: ^(?!.*--)[A-Za-z\d-]+$ it accepts alphanumeric characters, optionally with single dash only and it does not accepts space. now i need it to accept space and only single space only for example: ab34 12435, ab34- 12435 is valid. ab34 12435, ab34- 12435 is invalid. never mind the space before and end of the example.. i can trim that before i validate it to my problem regular expression. please help kerigan | | tengtium | This pattern works for me:
^(?:\s?\w+[\s,-]?)*$
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com - Marked As Answer bytengtium Monday, September 07, 2009 1:40 AM
-
| | JohnGrove | Are you trying to match them individually or all together?
This: \w{2}\d{2}\s{1}\d{5},\s\w{2}\d{2}-?\s\w{5}
Will match: ab34 12435, ab34- 12435
But not: ab34 12435, ab34- 12435 John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | no sir... it needs to accept alphanumeric with optionally a non-consecutive dash and optionally a non-consecutive space. it accepts: 12a-3c-4f3fg 12ertgg2 1-2-3-3-4-3 dffgsfg d-f-f-g-s-f-g it does not accept: 12-3c-4f&3fg 12e%rt-gg2 d--f-f-g-s-f-g 1-2-3-3-4--3
kerigan | | tengtium | good morning sir, i'm using regexlib tool to test my regular expression. http://renschler.net/RegexBuilder/ when i try the said regular expression all the above sample are invalid. Does the Environment.NewLine gives unusually side effect. if simple regular expression won't do the work. i just include the code snippet in my CLR. you are very helpful sir, thank you sooo much. god bless kerigan | | tengtium | good morning sir. i got this: ^(?!.*--)[A-Za-z\s \d-]+$ it accept alphanumeric with non-consecutive dash. but the space is the problem.. it accepts a consecutive spaces. what i need is OPTIONALLY a non-consecutive. it accepts: 12a-3c-4f3fg 12ertgg2 1-2-3-3-4-3 dffgsfg d-f-f-g-s-f-g it does not accept: 12-3c-4f&3fg 12e%rt-gg2 d--f-f-g-s-f-g 1-2-3-3-4--3 1-2-3-3-4 3 -> THIS CONSECUTIVE SPACE kerigan | | tengtium | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String[] testWords = {"12a-3c-4f3fg",
"12ertgg2",
"1-2-3-3-4-3",
"dffgsfg",
"d-f-f-g-s-f-g",
"12-3c-4f&3fg",
"12e%rt-gg2",
"d--f-f-g-s-f-g",
"1-2-3-3-4--3"};
String pattern = @"^(?:\s?\w+[\s,-]?)*$";
Regex rx = new Regex(pattern, RegexOptions.IgnoreCase);
foreach (String word in testWords)
{
Match m = rx.Match(word);
if (m.Success)
Console.WriteLine("Success: {0}", word);
else
Console.WriteLine("Failed: {0}", word);
}
Console.ReadLine();
}
}
}
//Output Success: 12a-3c-4f3fg Success: 12ertgg2 Success: 1-2-3-3-4-3 Success: dffgsfg Success: d-f-f-g-s-f-g Failed: 12-3c-4f&3fg Failed: 12e%rt-gg2 Failed: d--f-f-g-s-f-g Failed: 1-2-3-3-4--3 - Proposed As Answer byJohnGrove Sunday, September 06, 2009 3:30 PM
-
| | JohnGrove | This pattern works for me:
^(?:\s?\w+[\s,-]?)*$
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com - Marked As Answer bytengtium Monday, September 07, 2009 1:40 AM
-
| | JohnGrove | thank you sooo much sir.. i really appreciate it.. good bless.. kerigan | | tengtium | tengtium, You didn't say, but based upon the fact that the pattern works for you, you do not expect to see input values like.. -abc def 123 abc -def 123 ...because if you do, you need to tweak John's pattern more. Furthermore, I thought you did not want to allow consecutive spaces? abc def 123 ...is successful when I understood this should fail.
Les Potter, Xalnix Corporation, Yet Another C# Blog | | xalnix | yes sir.. yes sir i know it does fail.. i decided to use two regular expresison: one the that checks the consecutive dash ^(?!.*--)[A-Za-z\d\-]+$ and the other that checks the consecutive space.. ^(?!.* )[A-Za-z\d\s-]+$ if input match the pattern ^(?!.*--)[A-Za-z\d\-]+$ then if input match the pattern ^(?!.* )[A-Za-z\d\s-]+$ then good input else bad input else bad input but in other forum i posted my problem, one of the great guys gave me this: ^(?!.*(--| ))[A-Za-z\d\s-]+$ thank you sir..
kerigan | | tengtium | tengtium,
You didn't say, but based upon the fact that the pattern works for you, you do not expect to see input values like..
-abc def 123 abc -def 123
...because if you do, you need to tweak John's pattern more. Furthermore, I thought you did not want to allow consecutive spaces?
abc def 123
...is successful when I understood this should fail.
Les Potter, Xalnix Corporation, Yet Another C# Blog
How about this Les: ^(?:\w+[\s,-]?)*$
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String[] testWords = {"12a-3c-4f3fg",
"12ertgg2",
"1-2-3-3-4-3",
"dffgsfg",
"d-f-f-g-s-f-g",
"12-3c-4f&3fg",
"12e%rt-gg2",
"d--f-f-g-s-f-g",
"1-2-3-3-4--3",
"-abc def 123",
"abc -def 123",
"abc def 123"};
String pattern = @"^(?:\w+[\s,-]?)*$";
Regex rx = new Regex(pattern, RegexOptions.IgnoreCase);
foreach (String word in testWords)
{
Match m = rx.Match(word);
if (m.Success)
Console.WriteLine("Success: {0}", word);
else
Console.WriteLine("Failed: {0}", word);
}
Console.ReadLine();
}
}
}
//Output
Success: 12a-3c-4f3fg
Success: 12ertgg2
Success: 1-2-3-3-4-3
Success: dffgsfg
Success: d-f-f-g-s-f-g
Failed: 12-3c-4f&3fg
Failed: 12e%rt-gg2
Failed: d--f-f-g-s-f-g
Failed: 1-2-3-3-4--3 Failed: -abc def 123 Failed: abc -def 123 Failed: abc def 123
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | How about this Les:
^(?:\w+[\s,-]?)*$
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
John, I tested your second pattern, it can't match ab34- 12345
I improved it to this pattern, it works fine, at least for present examples.
^(?:\w+-? ?)+$
I already tested it on my Easy GREP, here's the screenshot,
http://www.wonderstudio.cn/soft/grep/exp/090908.gif
www.wonderstudio.cn | | Eping Wang | I have tested and confirmed my patternworked on every pattern the user has demonstated. Then Les came along and enquired about more possible variations, so I honed the pattern to cover those.
I just checked my pattern against your variation you mentioned and it rejectedthat as it should. It is my understanding that ab34- 12345 should not match.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | EDIT: Forgive me Eping, I just saw what you meant.
I tested this and it seems ok
^(?:\w+-?,?\s?)*$ John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | ^(?!.*(--| ))[A-Za-z\d\s-]+$
kerigan
Actually, I think this is the best pattern for what you are doing. It is clearest reading in that it says... 1) Examine the whole string (^...$) 2) Don't allow consecutive dashes (?!.*(--|...)) 3) Don't allow consecutive spaces (?!.*(...| )) 4) Require that there be 1 or more letters, digits, dashes or spaces. It holds closest to your original request without excluding things you did not ask to exclude (like not allowing "- " or " -"). I would have altered the pattern a little for more clarity... ^(?!.*--)(?!.* )[A-Za-z\d\s-]+$ ... but the alternation works and is a matter of personal preference. Finally, watch out for \w as it includes more than just [A-Za-z\d], it might include things you don't want.
Les Potter, Xalnix Corporation, Yet Another C# Blog | | xalnix | Les, you could have done an IgnoreCase and simplified it even more:
^(?!.*--)(?!.* )[a-z\d\s-]+$
"Finally, watch out for \w as it includes more than just [A-Za-z\d], it might include things you don't want."
Expresso defines \w as "Alphanumeric", which is what it is generally interpreted to be. MSDN documentation merely calls it "Word character". I have heard you make that statement before on occasion Les. There is no need to get as precise as [A-Za-z\d] over \w unless the context calls for that.
None of OP examples, the ones that were suppose to pass and the ones that didn't, and this list keeps growing in an effort to try to show how much better everyone's pattern iscompared tomine by showing things the OP didn't show.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | John, My apologies, no offense intended. It's just that the OP started with letters, digits and dash, but \w also includes "_", so your pattern changed the original request. If he wanted "_", wouldn't he have provided one in the original example? Maybe the OP really does want "_", but if so maybe he should say so rather than giving a pattern that says he doesn't. I try to write patternsmatching what you want and excluding what you don't want. That's the kind of help I try to provide. Your style seems to be directly craft the pattern to the sample data. That is another kind of help that is just as useful as I believe mine to be. In the end, the user will be obliged to validate whatever advice he receives. As for the additional characters included in \w, the documentation is not explicit, so here's a program that shows the UTF8 characters in \w that are not in [a-zA-Z0-9]. It can be expanded beyond UTF8. And if someone is only concerned with ASCII, then they only need to know about "_".
string pattern1 = @"\w";
string pattern2 = "[a-zA-Z0-9]";
for (int i = 0; i < 256; ++i)
{
string test = new string(new char[] { (char)i });
if(Regex.IsMatch(test,pattern1) != Regex.IsMatch(test,pattern2))
Console.WriteLine("{0}:{1} {2}:{3} {4}:{5}", test, i,
pattern1, Regex.IsMatch(test,pattern1).ToString(),
pattern2, Regex.IsMatch(test,pattern2).ToString());
}
Les Potter, Xalnix Corporation, Yet Another C# Blog | | xalnix | Les, You know better then most people it is all a matter of knowing your data. All the examples provided by the user and even the examples you included didn't have any underscores in them so the inclusion of that in \wmattered little.
Yes, I realize you are somewhat of a perfectionist [which I like BTW, because I have learned so much from you], but I base the development of patterns onlyon what I have seen from the user, not on the unforeseen, unless it is something worth mentioning.
I agree that the inclusion of _ in \w is worth at least mentioning.
You didn't offend me, and I am sorry if I sounded rather defensive. It came out wrong... John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Hi, Les, About your pattern^(?!.*--)(?!.* )[A-Za-z\d\s-]+$ I don't have a Visual Studio, but I tested on several other tools, the result varies. On PowerGREP, it matched all lines, when I alter \s to space it matched the last two lines. On my Easy GREP, it matched only the last two lines On Regex Match Tracer, it matched total 9 lines. Here's my testing text: 12a-3c-4f3fg 12ertgg2 1-2-3-3-4-3 dffgsfg d-f-f-g-s-f-g 12-3c-4f&3fg 12e%rt-gg2 d--f-f-g-s-f-g 1-2-3-3-4--3 -abc def 123 abc -def 123 abc def 123 ab34 12435 ab34- 12435 I think only RegEx Match Tracer performed your pattern fully, but it also showed an error in your pattern, it matched lines with a hyphen-preceded text, like these two lines you have already indicated: -abc def 123 abc -def 123 By the way, I found Power GREP and Easy GREP neither can perform (?!.* ) properly, but they can perform (?!.*--) , a very strange problem. www.wonderstudio.cn | | Eping Wang | thank you sir.. kerigan | | tengtium | thank you all for all the comments and suggestion.. this forum is GREAT!!!. i really appreciate all the replies given.. thank you sir eping wang, john grove and xalnix.. kerigan | | tengtium | John, Yes, you must know "your" data, but from experience, the examples given are usually not sufficient to know "their" data. However, when they provide an example pattern and example data, I do my best to be sure my suggestion handles both the example data and the data "implied" by the example pattern. Of course, their pattern could be wrongly implying some data set (since they are here asking for help). But, on the other hand, their data could be wrong (as in incomplete), which many many threads in this forum prove often to be the case. So, which one should be addressed? Thankfully, there are many different people with many different styles providing many different approaches. I hope the OPs, can learn from us all.
Les Potter, Xalnix Corporation, Yet Another C# Blog | | xalnix | Eping, The different Regex's out there are mostly the same. First, consider how the match is being done by your tool. Are you taking all the lines and having the tool pick out the matches or are you giving your tool a line at a time and testing to see if it's valid. The pattern I provided plugs into an earlier sample program (byJohn)that tests for validity ofstrings in an array of strings. This is suggested by the "^...$" which is often used to verify a string is valid, rather than finding strings within a string. Of course, various options change the meaning of some of these zero width assertions, so ^ and $ are sometimes used for "finding" instead of validation. If you want a pattern that will find these kinds of strings, that's another discussion.
Les Potter, Xalnix Corporation, Yet Another C# Blog | | xalnix |
|