How to regex a phrase up to a parenthesis? - regex

I'm trying to regex a group called reason, i have got very close but can't quite figure out the last part. I want to regex everything between Reason: up to and not including the first bracket in (winRc=999)
The string that is being extracted is below.
Reason: The user name or password is incorrect. (winRc=999)
I wish to have an expression that shows:
A Full Match of "Reason: The user name or password is incorrect."
A Group 'Reason' Match of "The user name or password is incorrect."

you can use something similar to what #CodeManiac was mentioning above
/Reason: ([^(]*)/
A demo from regex101
https://regex101.com/r/J2ddFQ/1
The takes advantage of using a negative character class, very powerful.

Related

How to write regex for domain validation that will trigger pattern error in Angular form

I am validating a domain field in the form. I am using Validators.pattern(this.domainPattern) for doing that.
I am using below pattern:
public domainPattern: string = "^(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9]\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$";
It works fine for many cases. But when there is a white space in domain it is not triggering pattern error. What I am missing?
Quick help will be highly appreciated.
Thanks.
Try this pattern:
(?(?<= )(?=[^ ])|^)(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9]\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]
I just added (?(?<= )(?=[^ ])|^), conditional which checks:
first it checks condition (?<= ) if what is preceeding is space, if it is, then check if what's after is not a space with (?=[^ ]), if the condition fails, then check if we are at the beginning of a string with ^.
Demo
UPDATE
OP said:
I want user to enter just one valid domain name. If user enters "google.com google.com" it should be treated as invalid
Then you could use this pattern
^(?!.* .*)(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9]\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$
Just added (?!.* .*) which checks if there's sapce in following line, if it is, then it won't match anything, as space indicated multiple domain names.
Another demo

Regex to check if string contains not more than 2 repeated characters

I am new in Angular 4 and was developing sample application where I have user email id and password field.
In password field I have to check String must not contain more than 2 repeated characters and for that, I have made the following regex:
^(?:(.)(?!\1{2,}))*$
But this regex fails.
I also tried with the following regex:
^((.)(?!\2\2))*$
But it angular cli shows error:
Module parse error: Octal literal in strict mode
Can anyone help me in suitable regex for the above?
A slight variation of your first regex works:
^(?:(.)(?!\1\1))+$
See live demo.
You only need to check that there aren't two repeats, since that is true for all cases of "more than 1".
In English, the regex says "composed entirely of characters that aren't followed by 2 copies of themselves".

How to match username which is enclosed in special chars

I try to match the username of users on YouNow from a specific field.
I extracted this html, I try to extract the username _You Won
"\n\t\t\t\t\t\t14\n\t\t\t\t\t\t_You Won\n\t\t\t\t\t"
This is my regex attempt:
(\d+)[\\n\\t]+([\W\w]+[^\\n\\t"$])
This worked fine, first I match a number which is the level, then I match the username. However, if the username ends with either t or n then it does not get the last letter. So user game 1n would get cut down to game 1
Does someone know how I can fetch the username correctly?
Play it:
https://regex101.com/r/j8rufa/2
You could use Positive Lookahead at the end instead of [^\\n\\t"$].
Your code will be:
(\d+)[\\nt]+([\W\w]+(?=\\n\\t))
Demo: https://regex101.com/r/j8rufa/4
You can also use Positive Lookbehind to further enhance the code to ensure that the whole name is matched. For example, if the name is something like t_You Won, it will be matched without any issues:
(\d+)[\\nt]+(?<=\\t)([\W\w]+(?=\\n\\t))
Demo: https://regex101.com/r/j8rufa/6

Regex throws false error at specific input

I'd like to validate an email address input using the following regex:
^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$
However, it doesn't match an email in this format test#test.test-test.fr while it should be matched.
Could someone can get me a hint, where the problem is?
This should work :
^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,})$
The problem was the dash in the second part of the domain, it didn't matched the original pattern.

Regular Expression for some email rules

I was using a regular expression for email formats which I thought was ok but the customer is complaining that the expression is too strict. So they have come back with the following requirement:
The email must contain an "#" symbol and end with either .xx or .xxx ie.(.nl or .com). They are happy with this to pass validation. I have started the expression to see if the string contains an "#" symbol as below
^(?=.*[#])
this seems to work but how do I add the last requirement (must end with .xx or .xxx)?
A regex simply enforcing your two requirements is:
^.+#.+\.[a-zA-Z]{2,3}$
However, there are email validation libraries for most languages that will generally work better than a regex.
I always use this for emails
^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}" +
#"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
#".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
Try http://www.ultrapico.com/Expresso.htm as well!
It is not possible to validate every E-Mail Adress with RegEx but for your requirements this simple regex works. It is neither complete nor does it in any way check for errors but it exactly meets the specs:
[^#]+#.+\.\w{2,3}$
Explanation:
[^#]+: Match one or more characters that are not #
#: Match the #
.+: Match one or more of any character
\.: Match a .
\w{2,3}: Match 2 or 3 word-characters (a-zA-Z)
$: End of string
Try this :
([\w-\.]+)#((?:[\w]+\.)+)([a-zA-Z]{2,4})\be(\w*)s\b
A good tool to test our regular expression :
http://gskinner.com/RegExr/
You could use
[#].+\.[a-z0-9]{2,3}$
This should work:
^[^#\r\n\s]+[^.#]#[^.#][^#\r\n\s]+\.(\w){2,}$
I tested it against these invalid emails:
#exampleexample#domaincom.com
example#domaincom
exampledomain.com
exampledomain#.com
exampledomain.#com
example.domain#.#com
e.x+a.1m.5e#em.a.i.l.c.o
some-user#internal-email.company.c
some-user#internal-ema#il.company.co
some-user##internal-email.company.co
#test.com
test#asdaf
test#.com
test.#com.co
And these valid emails:
example#domain.com
e.x+a.1m.5e#em.a.i.l.c.om
some-user#internal-email.company.co
edit
This one appears to validate all of the addresses from that wikipedia page, though it probably allows some invalid emails as well. The parenthesis will split it into everything before and after the #:
^([^\r\n]+)#([^\r\n]+\.?\w{2,})$
niceandsimple#example.com
very.common#example.com
a.little.lengthy.but.fine#dept.example.com
disposable.style.email.with+symbol#example.com
other.email-with-dash#example.com
user#[IPv6:2001:db8:1ff::a0b:dbd0]
"much.more unusual"#example.com
"very.unusual.#.unusual.com"#example.com
"very.(),:;<>[]\".VERY.\"very#\\ \"very\".unusual"#strange.example.com
postbox#com
admin#mailserver1
!#$%&'*+-/=?^_`{}|~#example.org
"()<>[]:,;#\\\"!#$%&'*+-/=?^_`{}| ~.a"#example.org
" "#example.org
üñîçøðé#example.com
üñîçøðé#üñîçøðé.com