Restrict particular domain in email regular expression - java

I have an existing regex which validates the email input field.
[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!$%&'*+/=?^_`{|}~-]+)*(\\.)?#(?:[a-zA-Z0-9ÄÖÜäöü](?:[a-zA-Z0-9-_ÄÖÜäöü]*[a-zA-Z0-9_ÄÖÜäöü])?\\.)+[a-zA-Z]{2,}
Now, I want this regex to not match for two particular type of email IDs. Which are wt.com and des.net
To do that I made the following changes in the above expression like this.
[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!$%&'*+/=?^_`{|}~-]+)*(\\.)?#(?!wt\\.com)(?!des\\.net)(?:[a-zA-Z0-9ÄÖÜäöü](?:[a-zA-Z0-9-_ÄÖÜäöü]*[a-zA-Z0-9_ÄÖÜäöü])?\\.)+[a-zA-Z]{2,}
After this it does not matches with any email id which ends with the wt.com and des.net which is right.
But the problem is it does not match with wt.comm or any other letter after the restricted string too..
I just want to restrict email which ends with wt.com and des.net
How do I do that?
Below is the sample emails which should match or not.
ajcom#wt.com : no match
ajcom#aa.an : match
ajcom#wt.coms :match
ajcom#des.net : no match
ajcom#des.neta: match

If you want to prevent only wt.com and des.net which have no characters after it you can add $ anchor (which represents end of string) at the end of each negative-look-ahead.
So instead of (?!wt\\.com)(?!des\\.net) use (?!wt\\.com$)(?!des\\.net$)

Related

java.util.regex.PatternSyntaxException: Illegal repetition near index 12

I am new to regEx. I need to validate email using java. I have created regEx for email validation by hardcoding the domain name. But the domain name should be dynamic. I have passed the domain name as a parameter. But I don't know how to pass parameter in regEx.
But I tried this code, then I got the error "java.util.regex.PatternSyntaxException: Illegal repetition near index 12". I have followed some answers but it doesn't help for me. From those answers I understood about repetition quantifier. Can you tell me what I am missing here and how to solve this issue?
public static boolean validateEmail(String email, String domainName) {
pattern = Pattern.compile("^([\\w-\\.]+)# {"+ domainName +"}" , Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(email);
return matcher.matches();
}
{ and } have meaning in regex, namely for specifying how often the character before it can repeat. E.g. a{5} matches aaaaa.
If you want to use curly braces in regex, you should escape them like \\{ and \\}.
But that's not what you need for passing this as a parameter — it will just be literal text at that point. If you want to only match that literal domain, you could do Pattern.compile("^([\\w-\\.]+)#" + domainName, Pattern.CASE_INSENSITIVE).

Spring boot Request Mapping URI Patterns

There is a request mapping like this:
#DeleteMapping(value = "/{version:.+}")
I not sure what is the .+ does, but from what i know, this delete mapping can accept a value and match to path variable version, something like:
DELETE
/abc
Value abc will map to path variable version
Why the .+ is needed?
Edited Question:
What is the difference with just /{version}, is there any special case that requires .+?
You can find details or URL matching on this link
URL matching
REGEX: .+ means one or more.
‘*’ Matches 0 or More Characters
'+' Matches 1 or More.
#DeleteMapping(value = "/{version:.+}")
.+ means "one or more of any characters" - thats standard regex/
version: means - put that match in path variable named version.

neo4j escaping for regular expressions

I receive a user input keyword and want to use it to search my database. I built a query that looks something like this:
db.execute("MATCH (n:User) WHERE n.firstname CONTAINS {keyword} OR n.lastname CONTAINS {keyword} RETURN n.username", params);
But this isn't case sensitive, so I thought of manually building the expression and using regular expressions, sort of as follows:
db.execute("MATCH (n:User) WHERE n.firstname =~ '(?i).*" + keyword + ".*' OR n.lastname =~ '(?i).*" + keyword + ".*' RETURN n.username");
I'm looking either for a function for escaping the regex or a better solution for making the query case-insensitive. Any ideas?
I would suggest storing the properties as all lowercase (or uppercase) and then using the Cypher lower() function to convert user input to lowercase for comparison.
Add lowercase name properties
MATCH (n:User)
SET n.lowerFirstName = lower(n.firstname),
n.lowerLastName = lower(n.lastname)
Find lower case matches based on user input
db.execute("MATCH (n:User) WHERE n.lowerFirstName CONTAINS lower({keyword}) OR n.lowerLastName CONTAINS lower({keyword}) RETURN n.username", params);

Regex for parsing string with same type of expression

I am new to regex parsing in java. I want to parse the string which contain the records. But I want to select the selected part of that record only.
\"6\":\"Services Ops\",\"practice_name\":\"Services Ops\",\"7\":\"Management\",
For this, I have written regex expression as
(^\\\"6\\\":\\\"[A-Za-z \s]*)
and above expression gives me result as : \"6\":\"Services Ops\
I want only Service Ops
And also there are multiple records like \"5"\:\"xxx"\ and so on thus if I write the expression for only Service Ops then entries from other fields are also included in the result of the expression.
Is there any way that we can select the string which start with some pattern but we can exclude that pattern.
Like in above example, string starting with \"6\":\" but we can exclude this part and get only Service Ops as result.
Thank you.
You can use lookarounds which perform only a check but don't match:
lookahead (?=...)
lookbehind(?<=...)
example:
(?<=\\\"6\\\":\\\")[^\"]++(?=\")
An another way is to use a capturing group (...):
\\\"6\\\":\\\"([^\"]++)\"
Then you can extract only the content of the group. Example:
Pattern p = Pattern.compile("\\\"6\\\":\\\"([^\"]++)\"");
Matcher m = p.matcher(yourString);
if (m.matches()) {
System.out.println(m.group(1));
}

Java Regular Expression to Handle Strings Contains Next Line

I have a text like this
Customer Owned 03/26 04/25 0.00
Modem
Here Modem is in Next line
Now i need to write the data into spreadsheet as
Customer Owned Modem 03/26 04/25 0.00
I wrote a regex as
([a-zA-Z = ]*) ([[0-9]{2}/[0-9]{2} ]*) (-?[0-9]*\\.[0-9]+)
I am getting the description as "Customer Owned" instead of "Customer Owned Modem". Is there any way to handle through Regex?
You could try this regex:
([A-Za-z ]+)([^A-Za-z]+)[\r\n]*([A-Za-z]+)
And replace by:
\1\3 \2
Here's a demo using your example.
To match newline you can try \r?\n
Update your regex accordingly to include newline as well as the text thereafter
Please add the following to your regular expression. This will detect an end of line on all platforms and capture the following line in the last group. You can then concatenate group 1 and the last group together.
(?:\n|\r|\n\r|\r\n)([a-zA-Z = ]*)$

Categories

Resources