I'm trying to find out how to remove all invalid characters in an email address.
Ex: email="taeo͝';st#yy.com"(. is an email character) and the result should be: email = "taest#yy.com"
I'm using the following email pattern:
String email_pattern = "^[^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#"+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$]";
String modifiedEmail = email.replaceAll(email_pattern,"");
But the above code is giving the result: email = "aest#yy.com" but expected "taest#yy.com"
Any suggestions or a better approach would be appreciated.
Here is a nice blog post of why you shouldn't filter your email adresses:
http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/
TL;DR: Check if there is an # (optionally a period) and send a test mail.
David suggests to use this regular expression:
/.+#.+\..+/
I got it resolved by using pattern matcher.
email = "testo͝';#.com.my"
String EMAIl_PATTERN = "[^a-zA-Z0-9!#$%&#'*+-/=?^_`{|}~.]+";
modifiedEmail = email.replaceAll(EMAIl_PATTERN, "");
Further thinking:
You could also be testing for know providers of email adresses used without authentication (e.g. http://trashmail.com/).
Related
I've tried to write a java client server application for chatting between clients privately. I am searching for a way to write the command to send a message to one or more receivers, I thought of handling it with a command like:
/send UserName UserName msg
but I pretty soon discovered that the character in between usernames and the message couldn't be repeated in the message because then it wouldn't know what is the message and what are the recipients, using a prearranged character also seemed to have some implications, like the inability to use that character in usernames or the message.
What would be a good way to handle such a thing?
You can determinate what is message and what are usersNames by splitting on last and first occurrence of delimiter in your case empty string. Lets say user input is inputStr
int firstOccurance = inputStr.indexOf(" ");
int lastOccueance = inputStr.lastIndexOf(" ");
And split it like this
String command = inputStr.substring(0,firstOccurance); // /send
String users = inputStr.substring(firstOccurance+1,lastOccurance); //UserName UserName
String msg = inputStr.substring(lastOccurance+1); //msg
Using space as delimiter will lead to a issue that message itself cannot include a space.
Instead you can use special delimiter to indicate a particular substring is a message.
Some very common delimiters are | ;
I want to explain my problem about a command string submitted from the client to the server. Through this specific command I have to login into the database managed by the server. According to the project's guide lines, command's formatting is: login#username;password. The string is sent to the server through a socket. User inserts his credentials and these are sent to the server putting them into the command. The server has to split the user and the password from the command and has to check, in the database, if the user already exists. Here is my problem, given the fact that in the user and in the password can be present ; (the same character of the separator), I don't understand (server side) how can i divide user and psw from the command so that user and psw are the same of the ones inserted in the client.
Have you got some ideas about? I was thinking to use regular expression, is this the right way?
I would just split the string into a user/pass string like this:
String userPass = "User;Pass";
String[] parts = userPass.split(";");
String user = parts[0];
String pass = parts[1];
If you really have to split a string by a separator included in both substrings there is no way to make sure the string is always split correctly!
I think you can use the unicode unit separator character 0x001F to separate you strings safely, as the user will have some difficulties entering such a character!
But depending on your application and string processing this could cause damage, too, as there are some issues concerning incompatibilities (e.g. xml 1.0 doesn't support it at all).
Otherwise if only one or none of the substrings may contain such a character you can easily use one of the already presented methods to split up the string and safely extract the data.
This will work only if User/password doesn't contain # or ;
String loginStr = "login#User;Password";
String []splittedLogin = loginStr.split("#");
String []loginCredentials = splittedLogin[1].split(";");
String user = loginCredentials[0];
String password = loginCredentials[1];
System.out.println(user);
I'm currently in the progress of making a java application, one of the functions is showing related emails and documents.
But the full path to the email (on a sharepoint server) is displayed in the application, for obvious reasons the number of characters depends on the title of the email and the location.
But they all have the same in common, there are ALWAYS 3 slashes in the title.
Like this: Myserver/client/caseID/Title of Email here
Is it possible to get Java to "count" the number of slashes and just delete everything before the third slash?
Use the Split function to achieve this.
String value="Myserver/client/caseID/Title of Email here";
value=value.split("\\/")[3];
System.out.println("your value is "+value);
Here is an example:
String s = "Myserver/client/caseID/Title of Email here";
int i = s.lastIndexOf('/');
if (i != -1)
System.out.println(s.substring(i));
else
System.out.println("no slashes");
Using the replaceFirst function is one way to go:
String yourString = "Myserver/client/caseID/Title of Email here";
System.out.println(yourString.replaceFirst("([^/]+/){3}", ""));
try this
s = s.replaceFirst(".*?/.*?/.*?(?=/)", "");
I have a log file that contains sent and received messages. I want to extract these message using a regular expression. This is an example of log file:
Message recieved:0908082349234 Session: ...
A message is sent: 12344384834
I wrote the following regex to extract the message:
String pattern = "((A message is sent: )|(Message received:))(.*)(( Session:)|$)";
And it doesn't work. I tried many different forms of it but none of them worked. I want to do this using a single regex. Right now I use one regex for sent and another one for received. But there should be a way to use a single regex for both of them!
.* matches greedily. Use non-greedy version (.*?):
String pattern = "(A message is sent: |Message received:)(.*?)( Session:|$)";
BTW, the given log file contains a typo. If the log file really contains the typo, you should adjust the regular expression accordingly.
Message recieved:0908082349234 Session: ...
^^
try this pattern
"((A message is sent: )|(Message recieved:))(\\d+?)(.*)"
btw. it seems that you 'recieved' is miss matching between your log and your pattern
If my domain is 31.example.appspot.com (App Engine prepends a version number), I can retrieve the domain from the Request object like this:
String domain = request.getServerName();
// domain == 31.example.appspot.com
What I want is to extract everything except the version number so I end up with two values:
String fullDomain; // example.appspot.com
String appName; // example
Since the domain could be anything from:
1.example.appspot.com
to:
31.example.appspot.com
How do I extract the fullDomain and appName values in Java?
Would a regex be appropriate here?
If you are always sure of this pattern, then just find the first dot and start from there.
fullDomain = domain.subString(domain.indexOf('.'));
UPDATE: after James and Sean comments, here is the full correct code:
int dotIndex = domain.indexOf(".")+1;
fullDomain = domain.substring(dotIndex);
appName = domain.substring(dotIndex,domain.indexOf(".",dotIndex));
Take a look at split method on java.lang.String.