I am working on an android app that summarizes text messages that are sent to clients from Financial services provider, a text message is sent as a notification each time a user makes a transaction.
Here is a sample message
[-ZAAD SHILLING-] Ref:1141125019 SLSH4,000 sent to AXMED XASAN
WARSAME(634458520) at 12/05/19 22:33:03, Your Balance is
SLSH44,222.62.
So I want to extract several portions of this message like
Ref:1141125019
Amount Sent: SLSH4,000
Recipient Name: AXMED XASAN WARSAME
Recipient Phone: 634458520
Date: 12/05/19
Time: 22:33:03
Balance: SLSH44,222.62
I have already got the text messages to appear in a listview, I now want to customize it, I don't want the whole message to appear, I just want the portion I mentioned above to appear.
Here is a Sample Code
if (Data.contains("Ref:")){
String[] Tx = Data.split("Ref:");
String TxID = Tx[1];
}
This problem would probably be best solved using regular expressions (regex)
Regular expressions allow you to match a string based on a pattern, and extract information from the string.
public static void main(String[] args) {
String data = "( [-ZAAD SHILLING-] Ref:1141125019 SLSH4,000 sent to AXMED XASAN WARSAME(634458520) at 12/05/19 22:33:03, Your Balance is SLSH44,222.62. )";
String headerReg = "\\[-([a-zA-Z\\s]+?)-]";
String refReg = "Ref:([0-9]+)";
String amountReg = "([,.\\w]+)";
String nameReg = "([\\w\\s]+?)";
String accountReg = "\\([0-9]+\\)";
String dateReg = "([0-9]{2}/[0-9]{2}/[0-9]{2})";
String timeReg = "([0-9]{2}:[0-9]{2}:[0-9]{2})";
String balanceReg = "([,.\\w]+?)";
String finalReg = "\\( " + headerReg + " " + refReg + " " + amountReg + " sent to " + nameReg + accountReg + " at " + dateReg + " " + timeReg + ", Your Balance is " + balanceReg + ". \\)";
Pattern pattern = Pattern.compile(finalReg);
Matcher matcher = pattern.matcher(data);
if (matcher.find()) {
MatchResult result = matcher.toMatchResult();
int groups = result.groupCount();
for (int i = 0; i < groups; i++) {
System.out.println(result.group(i + 1));
}
}
}
Using this, we can find the relevant data from your input string.
If the message syntax is always the same then you can use some tricky split strings like this:
String msg = "( [-ZAAD SHILLING-] Ref:1141125019 SLSH4,000 sent to AXMED XASAN WARSAME(634458520) at 12/05/19 22:33:03, Your Balance is SLSH44,222.62. )";
String ref = msg.split("Ref:")[1].split(" ")[0];
String amount = msg.split("Ref:")[1].split(" ")[1].split(" ")[0];
String recipient = msg.split("Ref:")[1].split("sent to ")[1].split("\\(")[0];
String phone = msg.split("Ref:")[1].split("sent to ")[1].split("\\(")[1].split("\\)")[0];
String date = msg.split("Ref:")[1].split("sent to ")[1].split("\\(")[1].split(" at ")[1].split(" ")[0];
String time = msg.split("Ref:")[1].split("sent to ")[1].split("\\(")[1].split(" at ")[1].split(" ")[1].split(",")[0];
String balance = msg.split("Your Balance is ")[1].split("\\)")[0];
System.out.println("ref: "+ref);
System.out.println("amount: "+amount);
System.out.println("recipient: "+recipient);
System.out.println("phone: "+phone);
System.out.println("date: "+date);
System.out.println("time: "+time);
System.out.println("balance: "+balance);
Result is:
ref: 1141125019
amount: SLSH4,000
recipient: AXMED XASAN WARSAME
phone: 634458520
date: 12/05/19
time: 22:33:03
balance: SLSH44,222.62.
Related
I have a string, let's call it output, that's equals the following:
ltm data-group internal str_testclass {
records {
baz {
data "value 1"
}
foobar {
data "value 2"
}
topaz {}
}
type string
}
And I'm trying to extract the substring between the quotes for a given "record" name. So given foobar I want to extract value 2. The substring I want to extract will always come in the form I have prescribed above, after the "record" name, a whitespace, an open bracket, a new line, whitespace, the string data, and then the substring I want to capture is between the quotes from there. The one exception is when there is no value, which will always happen like I have prescribed above with topaz, in which case after the "record" name there will just be an open and closed bracket and I'd just like to get an empty string for this. How could I write a line of Java to capture this? So far I have ......
String myValue = output.replaceAll("(?:foobar\\s{\n\\s*data "([^\"]*)|()})","$1 $2");
But I'm not sure where to go from here.
Let's start extracting "records" structure with following regex ltm\s+data-group\s+internal\s+str_testclass\s*\{\s*records\s*\{\s*(?<records>([^\s}]+\s*\{\s*(data\s*"[^"]*")?\s*\}\s*)*)\}\s*type\s*string\s*\}
Then from "records" group, just find for sucessive match against [^\s}]+\s*\{\s*(?:data\s*"(?<data>[^"]*)")?\s*\}\s*. The "data" group contains what's you're looking for and will be null in "topaz" case.
Java strings:
"ltm\\s+data-group\\s+internal\\s+str_testclass\\s*\\{\\s*records\\s*\\{\\s*(?<records>([^\\s}]+\\s*\\{\\s*(data\\s*\"[^\"]*\")?\\s*\\}\\s*)*)\\}\\s*type\\s*string\\s*\\}"
"[^\\s}]+\\s*\\{\\s*(?:data\\s*\"(?<data>[^\"]*)\")?\\s*\\}\\s*"
Demo:
String input =
"ltm data-group internal str_testclass {\n" +
" records {\n" +
" baz {\n" +
" data \"value 1\"\n" +
" }\n" +
" foobar {\n" +
" data \"value 2\"\n" +
" }\n" +
" topaz {}\n" +
" empty { data \"\"}\n" +
" }\n" +
" type string\n" +
"}";
Pattern language = Pattern.compile("ltm\\s+data-group\\s+internal\\s+str_testclass\\s*\\{\\s*records\\s*\\{\\s*(?<records>([^\\s}]+\\s*\\{\\s*(data\\s*\"[^\"]*\")?\\s*\\}\\s*)*)\\}\\s*type\\s*string\\s*\\}");
Pattern record = Pattern.compile("(?<name>[^\\s}]+)\\s*\\{\\s*(?:data\\s*\"(?<data>[^\"]*)\")?\\s*\\}\\s*");
Matcher lgMatcher = language.matcher(input);
if (lgMatcher.matches()) {
String records = lgMatcher.group();
Matcher rdMatcher = record.matcher(records);
while (rdMatcher.find()) {
System.out.printf("%s:%s%n", rdMatcher.group("name"), rdMatcher.group("data"));
}
} else {
System.err.println("Language not recognized");
}
Output:
baz:value 1
foobar:value 2
topaz:null
empty:
Alernatives: As your parsing a custom language, you can give a try to write an ANTLR grammar or create Groovy DSL.
Your regex shouldn't even compile, because you are not escaping the " inside your regex String, so it is ending your String at the first " inside your regex.
Instead, try this regex:
String regex = key + "\\s\\{\\s*\\n\\s*data\\s*\"([^\"]*)\"";
You can check out how it works here on regex101.
Try something like this getRecord() method where key is the record 'name' you're searching for, e.g. foobar, and the input is the string you want to search through.
public static void main(String[] args) {
String input = "ltm data-group internal str_testclass { \n" +
" records { \n" +
" baz { \n" +
" data \"value 1\" \n" +
" } \n" +
" foobar { \n" +
" data \"value 2\" \n" +
" }\n" +
" topaz {}\n" +
" } \n" +
" type string \n" +
"}";
String bazValue = getRecord("baz", input);
String foobarValue = getRecord("foobar", input);
String topazValue = getRecord("topaz", input);
System.out.println("Record data value for 'baz' is '" + bazValue + "'");
System.out.println("Record data value for 'foobar' is '" + foobarValue + "'");
System.out.println("Record data value for 'topaz' is '" + topazValue + "'");
}
private static String getRecord(String key, String input) {
String regex = key + "\\s\\{\\s*\\n\\s*data\\s*\"([^\"]*)\"";
final Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
//if we find a record with data return it
return matcher.group(1);
} else {
//else see if the key exists with empty {}
final Pattern keyPattern = Pattern.compile(key);
Matcher keyMatcher = keyPattern.matcher(input);
if (keyMatcher.find()) {
//return empty string if key exists with empty {}
return "";
} else {
//else handle error, throw exception, etc.
System.err.println("Record not found for key: " + key);
throw new RuntimeException("Record not found for key: " + key);
}
}
}
Output:
Record data value for 'baz' is 'value 1'
Record data value for 'foobar' is 'value 2'
Record data value for 'topaz' is ''
You could try
(?:foobar\s{\s*data "(.*)")
I think the replaceAll() isn't necessary here. Would something like this work:
String var1 = "foobar";
String regex = '(?:' + var1 + '\s{\n\s*data "([^"]*)")';
You can then use this as your regex to pass into your pattern and matcher to find the substring.
You can simple transform this into a function so that you can pass variables into it for your search string:
public static void SearchString(String str)
{
String regex = '(?:' + str + '\s{\n\s*data "([^"]*)")';
}
I am having a problem passing Strings to a server / client simple example pay system. Basically I can pass double/s however I would like to send and recieve the Employee name also this is my code and it doesn't work, can anyone help or suggest something, thank you
MY CODE:
if (e.getSource() == calculate) {
try {
//Get the employee name, employee id, payrate & hours from the text fields
String empname = jtfEname.getText();
double empid = Double.parseDouble(jtfEid.getText().trim());
double payrate = Double.parseDouble(jtfPayRate.getText().trim());
double hours = Double.parseDouble(jtfHours.getText().trim());
//send the employee name, employee id, payrate & hours to the server
outputToServer.writeString(empname);
outputToServer.writeDouble(empid);
outputToServer.writeDouble(payrate);
outputToServer.writeDouble(hours);
outputToServer.flush();
//Get pay from the server
double pay = inputFromServer.readDouble();
//Display to the text area
jta.append("Employee Name is " + jtfEname + "\n");
jta.append("Employee ID is " + jtfEid + "\n");
jta.append("PayRate is " + jtfPayRate + "\n");
jta.append("Hours is " + jtfHours + "\n");
jta.append("Pay received from the server is " + pay + '\n');
jtfPayRate.setText("");
jtfHours.setText("");
}
You should use DataInputStream and DataOutputStream.
Then you can simply use the readUTF() and writeUTF() methods:
// Client: send String
DataOutputStream out = ...;
out.writeUTF("Some string");
// Server: receive String
DataInputStream in = ...;
String text = in.readUTF();
I have one String text that i would like to split,result i want to get is that when i take text/split output each part like for example: Name: John, Last Name: Davidson, Date of Birth: 05051968, Place of Birth: London. But i am not getting correct result. my code is following:
public class Person{
public String name;
public String lastName;
public String dateOfBirth;
public String placeOfBirth;
poblic void printDetails(){
String text = "John.Davidson/0505168/London Micheal.Bartson/06061680/Paris";
String[] newText = text.split("[./ ]");
for(int i=0; i<newText.length; i++){
String name = newText[i].split("")[0];
String lastName = newText[i].split("")[0];
String dateOfBirth = newText[i].split("")[0];
String placeOfBirth = newText[i].split("")[0];
System.out.println("Name: " + name + ", last name: " + lastName + ", date of birth: " + dateOfBirth + ", place of birth: " + placeOfBirth);
}
Result i am getting is following:
Name: J, last Name: J, date of birth: J, place of birth: J
Name: D, last name: D, date of birth: D, place of birth: D .......
and it goes like that for every first character in text. Please can some one look and tell me where i am mistaking?
The results of the split come in groups of four, so you need to set the step of your loop at 4, and get the individual parts through offsets 0, 1, 2, and 3, like this:
for(int i=0; i<newText.length; i+=4){
String name = newText[i];
String lastName = newText[i+1];
String dateOfBirth = newText[i+2];
String placeOfBirth = newText[i+3];
System.out.println("Name: " + name + ", last name: " + lastName + ", date of birth: " + dateOfBirth + ", place of birth: " + placeOfBirth);
}
Demo.
You're splitting using the "" which means split every character. Then you take the first character. I don't know why you're doing it that way.
In summary, what happens in every loop is it takes the first character ([0]) of element i of the array, then sets every single value that wil lbe printed in the string to that character. Instead, try this
String[] newText = text.split("[./ ]");
for(int i = 0; i < newText.length - 4; i+=4){
System.out.println("Name: " + newText[i] + ", last name: " + newText[i+1] + ", date of birth: " + newText[i+2] + ", place of birth: " + newText[i+3]);
}
However, this is a terrible solution, it relies on fixed sized entries and should not be used in practice. What if someone enters the string in a different order, or with one too many inputs or one too few? Try using more flexible designs, such as the usage of a csv format parser, so you always split using commas, and the rows can be something like
entry-type, entry
entry-type, entry2
entry-type, entry3
Or something like that. Try it out. Always try to aim for flexible solutions that don't rely on exact input to work, otherwise you will have exceptions and runtime issues like there's no tomorrow.
PS the point of the split() method is to split the string between occurences of the specified input, i.e. [./], so don't use it if you want to just give a "", that's no different than making a charArray (except instead of char[] it is String[])
Is there a way to use LDAP attributes stored in Strings as the RegEx pattern?
I am thinking something like this:
PASSWORD_WITH_LDAP_ATTRIBUTE = Pattern.compile(userid + "|" + ssn + "|" + bdate + "|" + empNo + "|" + telNo);
I already tried this one out. In any string that I input, the regex always finds a match even though its not a clear match.
Is this possible or am I venturing to the impossible?
Here is the whole method:
private static Pattern PASSWORD_WITH_LDAP_ATTRIBUTE = null;
private boolean checkForLdapAttributes(final String newPassword) throws LDAPException{
LoggingEnt loggingEnt = new LoggingEnt();
String userid = loggingEnt.getUseridCode();
String ssn = loggingEnt.getSocialSecurityNumber();
String bdate = loggingEnt.getBirthDate();
String empNo = loggingEnt.getEmployeeNumber();
String telNo = loggingEnt.getTelephoneNumber();
PASSWORD_WITH_LDAP_ATTRIBUTE = Pattern.compile(userid + "|" + ssn + "|" + bdate + "|" + empNo + "|" + telNo);
matcher = PASSWORD_WITH_LDAP_ATTRIBUTE.matcher(newPassword);
if(PASSWORD_WITH_LDAP_ATTRIBUTE.matcher(newPassword).find()){
isPasswordAccepted = false;
loggingEnt.setMsg1("You cannot use any of your Username, Social Security No., Birthdate, Employee No., and Telephone No. as password.");
throw new LDAPException("Invalid password combination for " + userid, LDAPException.INVALID_CREDENTIALS);
} else {
loggingEnt.setMsg1("Password accepted.");
isPasswordAccepted = true;
}
return matcher.matches();
}
You're doing this wrong. You're not supposed to match the password attribute yourself. You are supposed to attempt to 'bind' to LDAP specifying a username and password, and it will match the password for you. Or not.
In JNDI, 'bind' corresponds to LdapContext.reconnect().
I have a number of xml's that come in haphazardly that contain a Ocount, and Lnumber, as well as other data. I have created a class to get that data.
My problem is that how can I group xml's that have the same Lnumber(string), until it reaches the Ocount(int). (the xmls that have the same lnumber has the same Ocount). And eventually send out a email telling with xmls has been processed.
String readLine = FileHandler.checkListFile(sh.getShipmentHeader().getBillToCustomer());
if (!readLine.isEmpty())
{
int orderCount = 0;
int index = readLine.indexOf(";") + 1;
String customerName = readLine.substring(index, readLine.indexOf(";", index)).trim();
index = readLine.indexOf(";", index) + 1;
String to = readLine.substring(index, readLine.length()).trim();
if (!billMap.containsKey(sh.getShipmentHeader().getBillToCustomer()))
{
billMap.put(sh.getShipmentHeader().getBillToCustomer(), 1);
orderCount = 1;
}
else
{
billMap.put(sh.getShipmentHeader().getBillToCustomer(), ((int) billMap.get(sh.getShipmentHeader().getBillToCustomer())) + 1);
orderCount = (int) billMap.get(sh.getShipmentHeader().getBillToCustomer());
}
outboundMessage += sh.getShipmentHeader().getOrderNumber() + li ;
logger.info("On-Demand Outbound Export Info: " + orderCount + " processed out of " + sh.getShipmentHeader().getOrderCount() +
" for " + customerName);
if (orderCount == sh.getShipmentHeader().getOrderCount())
{
Email email = new Email();
billMap.remove(sh.getShipmentHeader().getBillToCustomer());
outboundMessage += li + "Total of #"+ sh.getShipmentHeader().getOrderCount() + " orders processed for "+ customerName + li ;
logger.info("On-Demand Email sent for " + customerName);
System.out.println(outboundMessage);
email.outboundEmail("TEST: Orders for " + customerName + " complete", outboundMessage, to);
outboundMessage = "";
email = null;
}}
I been working on this for days, where am I going wrong.
It seems like you are having difficulty obtaining information from xmls. I suggest using XStream [1]. It is capable of serialising objects to xml and back. By using XStream, you can get an Object from the xml and compare variables (Lnumber and Ocount) easily.
If you insist using this code, I suggest adding comments to notify us what you are doing, but if want an easier alternative to work with xml files using java, I highly suggest using XStream as a solution.
[1] http://x-stream.github.io/