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>; 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 =
Related
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
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
So I am using this as a string:
Username entry \([a-z]{3,15}
To search this as an example:
[PA]apf_ms.c:7678 Username entry (host/computer.domain.com) is deleted for mobile a4:c4:94:63:1c:7a
[PA]apf_ms.c:7678 Username entry (username#domain.com) is deleted for mobile 94:e9:6a:ad:14:4d
Trying to wrap my head around regex and it's driving me nuts. My search only gets me so far, I am trying to make host/ optional and can't figure out where to insert it.
Username entry \((?:host/)?[a-z]{3,15}
(?: ... ) is a non-capture group
host/ is what you want to match
? after it means optional
You can use ? make anything optional in regex. The regex can be written as
Username entry \((?:host\/)?[a-z]{3,15}
(?:host\/)? Matches one or zero host/. The ?: within the brackets prevent it from capturing, as we don't want to save the host/ for future use.
Regex Demo
Currently i am writing a software where a user can input more than one email in a input field separated by: ";"
Now i have a regex that validates the email but sadly enough doesn't work when i have more Emails in the input field when using the separation.
Has anyone ever created such a regex or is there anyone that is able to help me?
Thanx in advance and looking forward for a response.
Here is my Regex:
[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+(\;|)
Just put the pattern which matches the following emails inside a non-capturing group with a preceding ; and make it to repeat zero or more times.
^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+(?:;[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+)*$
And one more thing is, you need to escape the dot.
Is there a way to differentiate between an email address and a twitter id?
Both use the '#' character and the email regex will be contained by the twitter id regex.
What's the best way to approach this?
Should I require a whitespace before the '#' character in order to identify that it's a twitter id?
Not entirely sure which characters are allowed in twitter usernames, but basically like so:
/(?:^|\s)#[a-zA-Z0-9_.-]+\b/
You can test that it's preceded by whitespace using (?<=\s) and then check for the valid characters of twitter IDs which are only [A-Za-z0-9_].
That gives you a resulting regex of: (?<=\s|^)#[A-Za-z0-9_]+
You could eventually add a check for a dot, comma or whitespace after it to check that it's properly formatted within a sentence and not some weird artifact:
(?<=\s|^)#[A-Za-z0-9_]+(?=[\s.,])
Note that the lookbehind and lookahead (?<= and ?=) might not work in your language of choice, but I'll assume it does since you didn't specify.
Email addresses never start with an #, while twitter ids always do.
isTwitter = address[0] == '#'
A twitter id wouldn't pass an email regex check.
Regular email:
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$
twitter won't have the last characters:
^#[A-Za-z0-9_]+$
So check if it's a valid email, if not, check if it's a valid twitter ID
Farther reading:
How to Find or Validate an Email Address