So my problem is I am looking to match a certain combination of letters at the start of an email address followed by an # and then a wildcard, for example:
admin#* OR noreply#* OR spam#* OR subscribe#
Can this be done?
Try this
^(?:admin|noreply|spam|subscribe)#\S*
See it here on Regexr
You need an anchor at the start to avoid matching address with other characters before. If the string contains only the email address use ^ at the beginning, this matches the start of the string. If the email address is surrounded by other text, the use \b this is a word boundary.
(?:admin|noreply|spam|subscribe) is a non capturing group, becuase of the ?: at the start, then there is a list of alternatives, divided by the | character.
\S* is any amount of non white characters, this will match addresses that are not valid, but should not hurt too much.
Your looking for grouping with the | operator. The following will do what you want.
edit: Since your using this for an email server rules you won't need to match the entire string, only part of it. In that case you will need to use ^ to specify the start of the string and then drop the domain portion since we don't care about what it is.
^(admin|noreply|spam|subscribe)#
sure thing!
[A-Za-z]+#.+
says any letters at least once but any number of times, then an at sign, then anything (other than newline) for your specific examples use
(admin|noreply|spam|subscribe)#.+
Related
I am trying to match a regex for an email address with the following conditions.
String must not contain more than 40 characters.
String matches the format emailid#domain where both emailid and domain contains only lowercase English letters, digits, and period(.)
domain should contain at least one period(.)
both email id and domain must not contain any consecutive period (.)
So Far, I am able to fulfill only the second condition with this regex:
/^[a-z0-9.]+#[a-z0-9.-]+\.[a-zA-Z]{2,6}$/
Any idea, how can I complete the other conditions?
You can use a single negative lookahead to make sure that the string does not contain 41 characters.
If you repeat the character class without a period 1 or more times, followed by optionally repeating a group that starts with a period, you prevent matching consecutive periods.
This part \.[a-zA-Z]{2,6}$ already makes sure that there is at least a single period.
^(?!.{41})[a-z0-9]+(?:\.[a-z0-9]+)*#[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-zA-Z]{2,6}$
Regex demo
Note that because the - is still in this character class [a-z0-9-]+, consecutive hyphens are still possible. If you don't want to allow this, you can use
^(?!.{41})[a-z0-9]+(?:\.[a-z0-9]+)*#[a-z0-9]+(?:[.-][a-z0-9-]+)*\.[a-zA-Z]{2,6}$
Use positive lookahead:
/(?=^[a-z0-9.]+#[a-z0-9.-]+\.[a-zA-Z]{2,6}$)(?=^.{1,40}$)/
reference: https://stackoverflow.com/a/469951/1031191
by Jason Cohen:
"(?=match this expression)(?=match this too)(?=oh, and this)"
tested on: https://regex101.com/
I am new to regex, i have this use case:
Allow characters, numbers.
Zero or one question mark allowed. (? - valid, consecutive question marks are not allowed (??)).
test-valid
?test - valid
??test- invalid
?test?test - valid
???test-invalid
test??test -invalid
Exlcude $ sign.
[a-zA-Z0-9?] - seems this doesn't work
Thanks.
Try the following regular expression: ^(?!.*\?\?)[a-zA-Z0-9?]+$
first we're using Negetive lookahead - which allows us to exclude any character which is followed by double question marks (Negetive lookahaed does not consume characters)
Since question mark has special meaning in regular expressions (Quantifier — Matches between zero and one times), each question mark is escaped using backslash.
The plus sign at the end is a Quantifier — Matches between one and unlimited times, as many times as possible
You can test it here
Your description can be broken down into the regex:
^(?:\??[a-zA-Z0-9])+\??$
You say characters and your description shows letters and numbers only, but it's possible \w (word characters) may be used instead - this includes underscore
It's between ^ and $ meaning the whole field must match (no partial matches, although if you want those you can remove this. The + means there must be at least one match (so empty string won't match). The capturing group ((\??[a-zA-Z0-9])) says I must either see a question mark followed by letters or just letters repeating many times, and the final question mark allows the string to end with a single question mark.
You probably don't want capturing groups here, so we can start that with ?: to prevent capture leading to:
^(?:\??[a-zA-Z0-9])+\??$
Matches
test
?test
?test?test
test?
Doesn't match
??test
???test
test??test
test??
<empty string>
?
I'm trying to modify my regex which requires user to type at least one letter and one digit which looks like this :
new RegExp('^(?=.*[a-z])(?=.*[0-9]).+$')
And I want to prevent user from using certain words like email address part before #.
So let's assume the email address is example#example.com
I want to force user to use a string that doesn't contain example in it (any part of the string)
This is what I have so far:
\b(?:(?!example)\w)+\b
But it doesn't really force the user to use at least one character and one digit.
When I'm trying to restric it I'm ending up with this:
\b(?:(?!example).*[a-z].*[0-9])+\b
But now the strings must follow the order of example then a [a-z] and then [0-9]
Any help greatly appreciated
Thanks!
In your sample the negative lookahead disallows example only at start of the string. Just add .*? as joker to disallow the word anywhere in the string and use word boundaries \b if needed.
/^(?!.*?example)(?=\D*\d)[^a-z]*[a-z].*$/i
(?!.*?example) first lookahead disallows example anywhere in the line
(?=\D*\d) second lookahead requires a digit after any amount of \D non-digits.
[^a-z]*[a-z] matches any amount of non-alphetic characters until an alphabetic.
See demo at regex101
Actually you just need two lookaheads for being independent of condition. One for the required digit and one for the word. The requirement of alphabetic can be done inside the pattern.
Lookarounds are zero-length assertions triggered at a certain position.
For example /(www\.)?(.+)(\.com)?/.exec("www.something.com") will result with 'something.com' at index 1 of the resulting array. But what if we want to capture only 'something' in a capturing group?
Clarifications:
The above string is just for example - we dont want to assume anything about the suffix string (.com above). It could as well be orange.
Just this part can be solved in C# by matching from right to left (I dont know of a way of doing that in JS though) but that will end up having www. included then!
Sure, this problem as such is easily solvable mixing regex with other string methods like replace / substring. But is there a solution with only regex?
(?:www\.)?(.+?)(?:\.com|$)
This will give only something ingroups.Just make other groups non capturing.See demo.
https://regex101.com/r/rO0yD8/4
Just removing the last character (?) from the regex does the trick:
https://regex101.com/r/uR0iD2/1
The last ? allows a valid output without the (\.com) matching anything, so the (.+) can match all the characters after the www..
Another option is to replace the greedy quantifier +, which always tries to match as much characters as possible, with the +?, which tries to match as less characters as possible:
(www\.)?(.+?)(\.com)?$
https://regex101.com/r/oY7fE0/2
Note that it is necessary to force a match with the entire string through the end of line anchor ($).
If you only want to capture "something", use non-capturing groups for the other sections:
/(?:www\.)?(.+)(?:\.com)?/.exec("www.something.com")
The ?: denotes the groups as non-capturing.
Okay, I have two Regex patterns.
([a-zA-Z0-9]?http[s]?:\/\/)?((?:(?:\w+)\.)(?:\S+)(?:\.(?:\w+))+?)
[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+.[a-zA-Z]{2,6}
The first meets my needs at finding web addresses in a string. The second meets my needs at locating email addresses in a string. However, for some reason the first one is finding email addresses that look like this first.last#d1.d2.d3.d4 or first.last#d1.com. I need some help getting that first one so that it doesn't pick up those email addresses.
For example you could fix it by excluding #
([a-zA-Z0-9]?http[s]?:\/\/)?((?:(?:\w+)\.)(?:[^\s#]+)(?:\.(?:\w+))*?)
and at the very end I suggest use *? instead of +?, +? didn't matched 1st level domain without www
yet it find abc#gmail.com
Sadly I have no idea how to check that 1st symbol before matched substring is not #
edit: bad solution
^[^#]*?([a-zA-Z0-9]?http[s]?:\/\/)?((?:(?:\w+)\.)(?:[^\s#]+)(?:\.(?:\w+))*?)
checks that there is no #s from the start of the line till matched part
([a-zA-Z0-9]?http[s]?:\/\/)?((?:(?:\w+)\.)(?:\S+)(?:\.(?:\w+))+?)
Breaking this down, there are several problems...
( // capture protocol
[a-zA-Z0-9]? // matches alphanumeric, optionally (do you really want that to start the string before the protoco?)
http[s]? // square brackets delimit character class, so are unneccessary here, although don't change functionality
:\/\/ // matches ://
)? // make captured protocol optional
((?:(?:\w+)\.)(?:\S+)(?:\.(?:\w+))+?) // too many lookaheads, not enough patterns. Innefficient and causing your error
I would replace the regex with something more like this...
(https?:\/\/)?(\w[-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?