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
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'm using the i18n Messaging of Playframework 2.4 java.
I've some messages in which I need to insert dynamic strings in runtime. Hence I've used placeholder strings for that surrounded with <> angular brackets
Following is how my messages.en file looks:
message.notification.eventOccured = '<PLACEHOLDER_STRING>' has occured. We''re sorry.
Now I read this message from my program with:
play.i18n.Messages.get("message.notification.eventOccured").replace("<PLACEHOLDER_STRING>", "Actual String");
My expected output is:
'Actual String' has occured. We're sorry.
But I'm getting:
'Actual String ' has occured. We're sorry.
There's a space character inserted just before the second single quote.
Why is this happening? Has it got something to do with angular brackets? Also if any workaround to get my desired output.
Thanks.
I'm developing an app for M2M communication via SMS between the mobile phone and an electronic device which has a SIM card.
When I send a command from the mobile to the device, this device returns a confirmation string to the mobile which has this structure.
Confirmation OK:
DEV33456, 15, Date: yy/mm/dd-hh:mm:ss. OK
Confirmation ERROR:
DEV33456, 15, Date: yy/mm/dd-hh:mm:ss. ERROR
Now in my app I have to manage this message to get the relevant information. For example, the first part of the message is the identify code (DEV33456) so to get it I split the message:
String[] separated = message.split(",");
String ident = separated[0]; //DEV33456
But now, I need to get the last word (OK or ERROR) to determine if the message is OK or not. To do that I thought that the easiest way should be to split the message using the point before the OK or ERROR word. Is the only point in the entire message, so it should split it like this:
String[] separated = message.split(".");
String unused = separated[0]; //DEV33456, 15, Date: yy/mm/dd-hh:mm:ss
String error = separated[1]; //OK or ERROR
But it is throwing an ArrayIndexOutOfBoundsException and debuging it I see that after the first line where the separated string array should have a lenght of 2, it has a lenght of 0.
Is there any problem on spliting the string using a "."? I have done the same procedure to split it using a "," and it has done int correctly.
Java split uses regular expressions, . is a special character in regular expressions.
You need to escape it by using the string "\\.".
why not just use
message.endsWith("OK")
Check if this is want you want:
if(message.substring(message.length()-2, message.length())
.toLowerCase().contains("ok"))
Log.d("yes");
else
Log.d("no");
Hopefully it is.
I am implementing urlReWriter into my Java web project.
I want to change this url: /read-post.jsp?id=1&title=some-cool-blog-title
into this shortened/cleaner url: /read-post/1/some-cool-blog-title
This is the rule I have implemented:
<rule>
<from>^/read-post/([0-9]+)/([0-9][a-z][A-Z]+)</from>
<to>/read-post.jsp?id=$1&title=$2</to>
</rule>
The problem is it isn't re writing the url and I suspect it is because the xml regex I've used is incorrect?
How do i format it correctly when there can be any number for the id and any number, character or special character - for the title?
Your regular expression for the title ([0-9][a-z][A-Z]+) is for sure not correct since the + refers to the [A-Z] only. In addition to this the - your are mentioning in the question is missing. You could try this instead: ([0-9a-zA-Z\-]+)
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/).