I have a custom regular expression attribute which implements IClientValidate so that I can use it with unobtrustive validate.
When I run it I get the following error in FireBug
SyntaxError: Invalid quantifier
match = new RegExp(params).exec(value);
It obviously does not like the regular expression that is passed to it, it is valid in C#. I can't seem to work out what I need to do to get it to be valid in JavaScript.
The regex is
^(?i)([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?\s?[0-9][ABD-HJLN-UW-Z]{2})$
The JavaScript regex flavor is extremely limited compared to .NET (C#). One of the many features it doesn't support is inline modifiers of the form (?i)regex or (?i:regex). However, because you're using the new RegExp(params) constructor, you should be able to pass the modifier as the second parameter:
"^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?\s?[0-9][ABD-HJLN-UW-Z]{2}$",
"i"
Replace (?i) by i modifier:
var patt=/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?\s?[0-9][ABD-HJLN-UW-Z]{2})$/i
When i run your regex in this : http://regexpal.com/
I get error on this part : (?i)
Rexegpal is a JavaScript regular expression tester.
So if you can get it to work here you can then apply it in your code.
Perhaps you have some example string to try with?
Related
I want to match all "the act" outside the tags in the *.sgm that my professor gave me, I know that we can use XML parser, but our goal is to learn REGEX purely.
this is my current Regex:
(?<![""=<\/])\bthe act\b(?!\>)
The problem is with this example:
<ptext>Test example the act example</ptext>
My regex matches "the act". And that is correct.
But if this example now I will try:
<ptext tags="Test the act">Example the act</ptext>
The regex will match (2) two "the act", the one that is inside the tag attribute and the one outside, I dont want to match all the act inside the tag, how can I do that? thanks.
Maybe this will work: (?<=\>[^>]*)the act(?=[^<]*\<) It should work if the regex engine allows variable length look behind, I think c#'s engine does.
I'm trying to pull out page source from a set of pages and run an assertion on the results, this is a Test that runs to check that we are crawling specific pages in our site. Sometimes the results come back with a different case for the URL string, I'd like to account for that in the Assertion where I am checking page source. This is probably the wrong way to do this but I was wondering if there is a way to add in the .Net regex commands to the Assertion text. I have this as an assertion:
Assert.IsTrue(driver.PageSource.Contains("/explore"));
But is there a way to be sure that I can capture explore, Explore or EXPLORE? I though I could use (?i) here but that doesn't seem to work. I'm more used to Perl and it's regex capabilities but with C# and .Net I'm a little lost on where I can and can't use the inline regex commands.
Anthonys answer is valid, you don't really need regex. But if you do want to use it, you can use
Regex.IsMatch(driver.PageSource, "/explore", RegexOptions.IgnoreCase)
You don't need a regular expression to perform a case-insensitive check. Use IndexOf and compare that the result is greater than -1. IndexOf has overloads that allow you to specify if casing matters. Something like
bool containsExplore = driver.PageSource.IndexOf("/explore", StringComparison.InvariantCultureIgnoreCase) > -1;
Assert.IsTrue(containsExplore);
Try:
RegEx.Match("string", "regexp", RegExOptions.IgnoreCase).Success
How about using
StringAssert.Matches(string, regex);
In your case, that would translate to
StringAssert.Matches("drive.PageSource", "\/explore");
I am playing around with the System.ComponentModel.DataAnnotations namespace, with a view to getting some validation going on my ASP.NET MVC application.
I have already hit an issue with the RegularExpression annotation.
Because these annotations are attributes they require constant expressions.
OK, I can use a class filled with regex string constants.
The problem with that is I don't want to pollute my regex with escape characters required for the C# parser. My preference is to store the regex in a resources file.
The problem is I cant use those string resources in my data annotations, because they are not constants!
Is there any solution to this?
If not, this seems a significant limitation of using attributes for validation.
In C# there is only one escape code you need (double-quote)... if you use verbatim string literals:
#"like \this\ note \slash here does nothing only quote "" needs doubling
you can even use newline";
I always write regex with #"..." strings - avoids many headaches.
Apparently in .NET 4 there are overrides for the DataAnnotations attribubtes that take a Func< string> in their constructor described as "The function that enables access to validation resources."
You could create a custom validation attribute like this as a proxy which would load the regular expressions from your resource file.
The theme is i opened a file and get all it's data into string and i am matching this string with the regex returning none. But the same regex in PHP is returning values for the same text using preg_match_all. Anyone having a idea?
The method in .NET that’s closest to preg_match_all() is the static Regex.Matches(String,String) call, or the equivalent Matches method on a compiled regular expression. It returns a MatchCollection that you can use to count the matches and to loop over each one.
Can you provide some short, self-contained code to show what’s not working?
There is a Regex.Matches method in C# that you can use.
I am wondering if it is possible to extract the index position in a given string where a Regex failed when trying to match it?
For example, if my regex was "abc" and I tried to match that with "abd" the match would fail at index 2.
Edit for clarification. The reason I need this is to allow me to simplify the parsing component of my application. The application is an Assmebly language teaching tool which allows students to write, compile, and execute assembly like programs.
Currently I have a tokenizer class which converts input strings into Tokens using regex's. This works very well. For example:
The tokenizer would produce the following tokens given the following input = "INP :x:":
Token.OPCODE, Token.WHITESPACE, Token.LABEL, Token.EOL
These tokens are then analysed to ensure they conform to a syntax for a given statement. Currently this is done using IF statements and is proving cumbersome. The upside of this approach is that I can provide detailed error messages. I.E
if(token[2] != Token.LABEL) { throw new SyntaxError("Expected label");}
I want to use a regular expression to define a syntax instead of the annoying IF statements. But in doing so I lose the ability to return detailed error reports. I therefore would at least like to inform the user of WHERE the error occurred.
I agree with Colin Younger, I don't think it is possible with the existing Regex class. However, I think it is doable if you are willing to sweat a little:
Get the Regex class source code
(e.g.
http://www.codeplex.com/NetMassDownloader
to download the .Net source).
Change the code to have a readonly
property with the failure index.
Make sure your code uses that Regex
rather than Microsoft's.
I guess such an index would only have meaning in some simple case, like in your example.
If you'll take a regex like "ab*c*z" (where by * I mean any character) and a string "abbbcbbcdd", what should be the index, you are talking about?
It will depend on the algorithm used for mathcing...
Could fail on "abbbc..." or on "abbbcbbc..."
I don't believe it's possible, but I am intrigued why you would want it.
In order to do that you would need either callbacks embedded in the regex (which AFAIK C# doesn't support) or preferably hooks into the regex engine. Even then, it's not clear what result you would want if backtracking was involved.
It is not possible to be able to tell where a regex fails. as a result you need to take a different approach. You need to compare strings. Use a regex to remove all the things that could vary and compare it with the string that you know it does not change.
I run into the same problem came up to your answer and had to work out my own solution. Here it is:
https://stackoverflow.com/a/11730035/637142
hope it helps