How to match username which is enclosed in special chars - regex

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

Related

How to regex a phrase up to a parenthesis?

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.

Get an exact regex match of an email value from a list of email addresses

I have a text field which stores a list of email addresses e.g: x#demo.com; a.x#demo.com. I have another text field which stores the exact value matched from the list of emails i.e. if /x#demo.com/i is in x#demo.com;a.x#demo.com then it should return x#demo.com.
The issue I am having is that if I have /a.x#demo.com/i, I will get x#demo.com instead of a.x#demo.com
I know of the regex expression /^x#demo.com$/i, but this means I can only have one email in my list of email addresses which won't help.
I have tried a couple of other regex expressions with no luck.
Any ideas on how I can achieve this?
You can use this slightly changed regex:
/(^|;)x#demo.com($|;)/i
It will match from either beginning of string or start after a semi colon and end either at end of string or at a semi colon.
Edit:
Small change, this uses look behind and look forward, then you will only get the match, you want:
(?<=^|;)x#demo.com(?=$|;)
Edit2:
To allow Spaces around the semi colon and at start and end, use this (#-quoted):
#"(?<=^\s*|;\s*)x#demo.com(?=\s*$|\s*;)"
or use double escaping:
"(?<=^\\s*|;\\s*)x#demo.com(?=\\s*$|\\s*;)"

How do i get password value in data connection using regex

I need to grab the value of password from db connection string using regex.
This is my current regex .*;(?i)Password=([^;]*).
This works only if there is not any ; character in password.
add key="myKey" value="Data Source=MyDataSource;Initial Catalog=MyDB;User ID=test-user;Password=pA&-pass; unicode=True"
But it fails if there is ; character in password
add key="myKey" value="Data Source=MyDataSource;Initial Catalog=MyDB;User ID=test-user;Password=pass&gt;; unicode=True"
Brief
There will always be ways for your code to break since someone can create a password such as ;Password= such that your string is actually ;Password=;Password=;.
Assuming that is not possible (and also assuming it's not possible for someone to use similar variations such as portions of the password being in the following format ;s= where s is any word or space character), this should work for you.
Code
See regex in use here
(?<=;Password=)(?:(?!;[\w ]+=).)*
Results
Input
add key="myKey" value="Data Source=MyDataSource;Initial Catalog=MyDB;User ID=test-user;Password=pA&-pass; unicode=True"
add key="myKey" value="Data Source=MyDataSource;Initial Catalog=MyDB;User ID=test-user;Password=pass>; unicode=True"
Output
pA&-pass
pass>
Explanation
(?<=;Password=) Positive lookbehind ensuring what precedes matches ;Password literally
(?:(?!;[\w ]+=).)* Tempered greedy token matching any character, but ensuring it doesn't match ;, followed by any word or space characters, followed by =

How to build this regex?

I want to match the emails in following texts,
uma#cs.stanford.edu - match
uma at cs.Stanford.edu - match
http://infolab.stanford.edu/~widom/yearoff.h
we
genale.stanford.edu
n <A href="mailto:cheriton#cs.stanford.edu - match
hola # kirti.edu - match
Now I want to capture 2 parts of email address only like (uma) and (cs.stanford) in the email uma#cs.stanford.edu.
My current pattern is :
(\w+)[(\s+at\s+)|(\s*#\s*)]+(\w+|\w+\.\w+).edu
But it matches the string - infolab.stanford.edu - which I don't want.
Can anybody suggest any modification on this?
As long as you understand that this regex doesn't verify the correctness of your email address, but merely acts as a quick first line of defense against malformed addresses, an easy fix to your regex is as follows:
([\w.]+)(?:\s+at\s+|\s*#\s*)(\w+|\w+\.\w+).edu
In particular your regex was missing addresses with usernames containing . (which for example my main email address uses), as well as had a messed up middle part (pretending it's a character class and something weird about letting it repeat??). You can see the results here: http://refiddle.com/2js1

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.