Regex c# to jquery implementation - c#

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.

Related

ASP.NET MVC RegularExpression Attribute Not Working with Parenthesis (literal)

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).

Displaying phone format XAML/c#

In my windows phone project, I would like the user to enter his phone number in xxx-xxx-xxxx format. The country code it not required. I tried to implement regex, but i am not getting it right. I just want it to be displayed to the user as he enters it, nothing more, nothing less. This is what I have used
^\(\d{3}\) ?\d{3}( |-)?\d{4}$
But no matter what i put in, I always get this error (in this case 5) "Unrecognized escape sequence". I noticed, this is only with reference to the oblique. When I add a "" after it, the error goes away, but I do not get what I want. Is there a special way to input numbers in the textbox in than manner, on the XAML level?
Thanks in advance!
Put your regex inside verbatim string and also put the space, hyphen inside a group and make it as optional.
#"^\(\d{3}\)([- ]?)\d{3}\1\d{4}$"
DEMO
For testing your RegEx you can use this site: http://www.regexlib.com/RETester.aspx?AspxAutoDetectCookieSupport=1.
For your xxx-xxx-xxxx format I would use it:^\d{3}-?\d{3}-?\d{4}$

How to replace entire URL using Regex? [duplicate]

This question already has answers here:
C# regex pattern to extract urls from given string - not full html urls but bare links as well
(3 answers)
Closed 9 years ago.
So far I have
messageText1 = Regex.Replace(messageText1, "(www|http|https)*?(com|.co.uk|.org)", "[URL OMITTED]");
With only the www, and without the bracks or http or https it works as intended
For example and input of Hey check out this site, www.google.com, it's really cool would output hey check out this site, [URL OMITTED], it's really cool
But if I put back in the or operators for the start of the URL, it only replaces the .com part of the input
Why won't it work?
Thanks
(www|http|https)*?(com|.co.uk|.org)
means www or http or https 0 to many times immediately followed by com .co.uk or .org.
So it would match for example httphttphttp.co.uk
Your intention was probably just to have a . before the *. Which then means it only looks for (www|http|https) once, then it matchs . (any character) 0 to many times.
You are also missing the . in .com. However, if you want to match a literal . you need to use \., since a . on its own means 'any character'.
With that in mind, the regex I think you were going for is:
(www|http|https).*?(\.com|\.co\.uk|\.org)
This should work better. It will also work for other TLDs that don't end with .com, .co.uk or .org:
messageText1 = Regex.Replace(messageText1, #"\b(?:http://|https://|www\.)\S+", "[URL OMITTED]");
Your expression is missing a . somewhere or (possibly better) a \S+
(www|http|https)\S*(com|\.co\.uk|\.org)
In C#:
Regex.Replace(messageText1, #"(www|http|https)\S*(com|\.co\.uk|\.org)", "[URL OMITTED]");
Note: you probably want to escape the .'s as well.
A simple version which i tried is as follows.
messageText1 = Regex.Replace(messageText1, #"(www)?(.)?[a-z]*.(com)", "[URL OMITTED]");
i tried this with
string messageText1 = " Hey check this out, http:\www.google.com,its cool";
string messageText1 = " Hey check this out, www.google.com,its cool";
string messageText1 = " Hey check this out, google.com,its cool";

Unable to get a block of code into my regex match groups

So yeah, the title is pretty weird but I have no other idea how to describe my problem properly. Whatever... lets get to the problem.
Job to get done
My boss wants a function that read all functions of a python file and return a DataTable containing the found functions. This function should be written in IronPython (Python which actually uses C# libraries).
The Problem
I am relatively new to Python and I have no idea what this language is capable of, so I started to write my function and yeah it works pretty well, except one weird problem. I wrote a regular expression to find the functions and to test it I downloaded a RegEx Tester. The Regex Tester showed the results I wanted: Group 1 - The function name, Group 2 - The functions parameter and Group 3 - the content of the function.
For some magical reasons, it doesn't work when it goes to live testing. And with doesn't work I mean, Group 3 has actually no output. After testing the expression with another (online) RegEx Tester, it showed me, that Group 3 has actually not the content of the function, it only has a small part of it, starting with a newline/return character.
In my test cases, the results of Group 3 where all the same, starting with a newline/return character and ended with the functions return (e.g. return objDic).
Question: What the hell is going wrong there? I have no idea what is wrong on my RegEx.
The Regex
objRegex = Regex(r"(?i)def[\s]+([\w]+)\(([\, [\w]+)\)(?:[\:{1}]\s*)([\n].*(?!\ndef[\s]+))+")
The Data
def test_function(some_parameter):
try:
some_cool_code_goes_here()
return obj
except Exception as ex:
DetailsBox.Show(ex)
def another_cool_function(another_parameter):
try:
what_you_want()
return obj
except Exception as ex:
DetailsBox.Show(ex)
The Result
Match: def test_function(some_parameter):...
Position: ..
Length: ..
Group 1: test_function
Group 2: some_parameter
Group 3: (newline/return character)
return obj
But Group 3 should be:
try:
some_cool_code_goes_here()
return obj
except Exception as ex:
DetailsBox.Show(ex)
I hope you can help me :3 Thank you guys!
Although #Hamza said in his comment that you have several problems in your regex, I think they are more of uneeded complexity, the reason for not matching the body might be that you haven't let the . special meta-character match the new line so it is stopping at the first new line character after the first Try: statement.
To fix this you will need to let the . match new line characters and here is a stripped down version of your regex that works:
(?i)def\s+(\w+)\s*\(([\, \w]+)\)(?:\s*:\s*)(.+?)(?=def|$)
Thanks to HamZa for the quick help (and of course also thanks for all the other helpers), he actually solved the problem. There were just a few adjustments necessary (to make it work for C# :-)) but the main point comes from him, thanks a lot.
Solution for my problem:
Regex(r"(?is)def\s*(?<name>\w+)\s*\((?<parameter>[^)]+)\)\s*:\s*(?:\r?\n)+(?<body>.*?)(?=\r?\ndef|$)")

How can I transform this url with REGEX?

I have a dynamic web app built using DotNetNuke that uses the following url format:
/SeoDummy.aspx?template={VAR1}&keywords={VAR2}
My user friendly url format is like this:
http://domain.com/.{VAR1}/{VAR2}
I am really terrible with REGEX and need to somehow detect when the user friendly url is requested and rewrite it with the dynamic web app url. I have tried the following, but It is not catching it on the site, it is just 404'ing:
.*/^([^/]+)/([^/]+)/?$
I am sure you that know regex will find my attempt silly, but regex is my kryptonite!
Thanks for any help that can be offered.
Since you are using some custom url,I guess regex would be better than using URI class
In your regex you have misplaced ^..The regex should be
^https?://domain[.]com/[.]([^/]+)/([^/]+)/?$
I have not tested this, but give it a shot and tell me how it works out:
domain[.]com/\.([^/]+)/([^/]+)/?$
It looks like you had it mostly right except for the first carat, marking the beginning of the string... which is impossible since you specified .* right in front of it! Also you missed the period in front of {VAR1} (unless that is a typo?).
I also wouldn't put .* at the beginning because then you could be capturing VAR1 = domain.com, VAR2 = something that is actually VAR1
If you want to become immune to your kryptonite, then this website is really good for looking up stuff:
http://www.regular-expressions.info/reference.html

Categories