Here's my sample regex in c#.
[RegularExpression(#"^[0-9]{4}-[0-9]{4}|\x28[0-9]{3,5}\x29$", ErrorMessage = "Invalid Format. xxxx-xxxx or xxxx-xxxx (xxxxx)")]
I've already tried this two regex but still not working.
^[0-9]{4}-[0-9]{4}|\([0-9]{3,5}\)$
^[0-9]{4}-[0-9]{4}|\x28[0-9]{3,5}\x29$
Here's the html output
data-val-regex-pattern="^[0-9]{4}-[0-9]{4}|\x28[0-9]{3,5}\x29$"
Desired Output Sample
1234-1234
1234-1234 (123)
1111-1111 (11111)
...
I'm testing the regex here at https://regex101.com/ and works as expected but not in my c# code.
Validation still triggers even the input is valid
The issue is that this regex does match, but it is not a FULL match - only partial. Use this:
^[0-9]{4}-[0-9]{4}( \([0-9]{3,5}\))?$
Your version was an alternative: either the first part OR the second part. My version is: always the first part AND MAYBE the second part (if present).
Related
Continuing from my previous question here: Need regular expression that extracts sub-strings that are in some specified format. The solution that was suggested there solved my problem but not completely. I came across another string sample from which the the sub-strings in following format must be extracted:
#[any character here].[any character here].[any character here]....... and so on.
For example, I have a string:
My location is #[XYZ Continent].[XYZ Country].[XYZ State].[XYZ City].[XYZ Street] on the map. You are most welcome there anytime.
Previous solution is working fine on this string. Output:
#[XYZ Continent].[XYZ Country].[XYZ State].[XYZ City].[XYZ Street]
But that solution only checks for + and whitespace outside the sub-string(CLICK HERE TO SEE DEMO). But what I want is that the right and left of the sub-string must be ignored no matter if it is alphnumeric, whitespace or another special character. For example:
Hello#[XYZ Continent1].[XYZ Country1]+#[XYZ Continent2].[XYZ Country2]1#[XYZ Continent3].[XYZ Country3]there.
The output must be:
#[XYZ Continent1].[XYZ Country1]
#[XYZ Continent2].[XYZ Country2]
#[XYZ Continent3].[XYZ Country3]
Solution from previous question:
(?<!\w)#(\w+\b|\[.*?\](?=[\s+]|$))
You can use the following:
(?<!\w)#((?:\[[^\]]+\]\.?)+)
See DEMO
I have this regex on my asp.net mvc3 application:
Regex pattern = new Regex(#"^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$");
I needed to implement this with jquery due to some requirements with something like this:
password.match(/(.*(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]/))
This is working. It will detect if 1 uppercase, 1 lowercase and 1 number is present on the password. However , i also have need to detect if 3 consecutive letter is present (eg: aaa, bbb).
With my regex on c# , it is working with the help of:
/(.)\1\1/
But I can't make it work on password.match(/(.)\1\1/)
Did I missed something here? Thanks in advance!
I've just copied your C# regex and tried in JavaScript console and it works great:
"waweEEad2".match(/^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$/)
returns ["waweEEad2", undefined] and
"waweEEEad2".match(/^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$/)
returns null.
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?
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.
We have implemented the invocation of brill tagger from our c# code. We just neede to know what is the correct Regex regular expression for eliminating all from a string, but jst keep a-z,A-Z, full stop and comma. We tried [^a-zA-Z\.\,] on the online regular expression tester and it is giving the correct result, but when implemented in C#, it is not working properly. We also tried several other combinations but we are not getting the correct result.
This is the format in which we are writing:
strFileContent = Regex.Replace(strFileContent, #"[^a-zA-Z\.\,]", "");
but we are not getting the desired output. what is wrong??
Regex.Replace(yourString, #"[^a-z\.\,]", string.Empty, RegexOptions.IgnoreCase)
EDIT: I can't see anything wrong with what you are doing, my answer is exactly the same. I tested both in LINQPad and they both return the same result.