I would like to use regex to parse a message received through a socket in an Android Client and put part of the message in a list.
This is the message to parse:
{Code=1;NumServices=3;Service1=World Weather Online;Link1=http://www.worldweatheronline.com/;Service2=Open Weather Map;Link2=http://openweathermap.org/;Service3=Weather;Link3=http://www.weather.gov/;}
and the method I'm using:
private void parse(String mess) {
String Code="0";
Pattern pattern = Pattern.compile("Code=(.*?);");
Matcher matcher = pattern.matcher(mess);
while (matcher.find()) {
Code = matcher.group(1);
Log.d("Matcher", "PATTERN MATCHES! Code parsed "+Code );
// System.out.println("Code: "+Code);
}
Log.d("Matcher", "PATTERN MATCHES! Code not parsed "+Code );
if(Code.compareTo("1")==0){
// System.out.println("testing the parser");
// Pattern pattern1 = Pattern.compile(";CPU=(.*?);Screen");
Pattern pattern2 = Pattern.compile("NumServices=(.*?);");
Matcher matcher2 = pattern2.matcher(mess);
int number=0;
if (matcher2.find()) {
String numb = matcher2.group(1);
this.tester = numb;
Log.d("Matcher", "PATTERN MATCHES! numb services");
number = Integer.parseInt(numb);
}
else{
this.tester = "NOT FOUND";
Log.d("Matcher", "PATTERN MATCHES! match num failed");
}
int i;
for(i=1;i<=number;i++){
Pattern pattern3 = Pattern.compile(";Service"+i+"=(.*?);");
Pattern pattern4 = Pattern.compile(";Link"+i+"=(.*?);");
Matcher matcher3 = pattern3.matcher(mess);
Matcher matcher4 = pattern4.matcher(mess);
if (matcher3.find()) {
// Log.d("Matcher", "PATTERN MATCHES! services");
String serv = matcher3.group(1);
// this.tester = serv;
your_array_list.add(serv);
}
if (matcher4.find()) {
Log.d("Matcher", "PATTERN MATCHES! links");
String link = matcher4.group(1);
your_array_list2.add(link);
}
}
}
}
None of the log.d works so I cannot verify the flow of the code. What's weird is that I tested the same code in Eclipse and it works. When I use toast to display, it gives me the value of Code, but not of Service. Is there an error somewhere or does regex work differently in Android?
Thanks.
You can actually use 1 regex to capture all pertinent data:
Code=([^;]*);NumServices=([^;]*);|Service(\d+)=([^;]*);Link\d+=([^;]*);
Here is a sample code:
String str = "{Code=1;NumServices=3;Service1=World Weather Online;Link1=http://www.worldweatheronline.com/;Service2=Open Weather Map;Link2=http://openweathermap.org/;Service3=Weather;Link3=http://www.weather.gov/;}";
Pattern ptrn = Pattern.compile("Code=([^;]*);NumServices=([^;]*);|Service(\\d+)=([^;]*);Link\\d+=([^;]*);");
Matcher matcher = ptrn.matcher(str);
while (matcher.find()) {
if (matcher.group(1) != null) {
System.out.println("Code: " + matcher.group(1));
System.out.println("NumServices: " + matcher.group(2));
}
else if (matcher.group(1) == null && matcher.group(2) == null) {
System.out.println("Service #: " + matcher.group(3));
System.out.println("Service Name: " + matcher.group(4));
System.out.println("Link: " + matcher.group(5));
}
}
See IDEONE demo
Related
I want to write java code to convert left side strings to right ones.
1234_hello -- 1234_Hello
hello Data -- Hello Data
hELLO data -- Hello data
1234hEllo -- 1234Hello
heLLO1234hEllo -- Hello1234hEllo
$hello -- $Hello
Could you please help with the solution?
Thank you!
Here is a solution:
public static void main(String[] args) {
try {
System.out.println(convertString("1234_hello"));
System.out.println(convertString("hello Data"));
System.out.println(convertString("hELLO data"));
System.out.println(convertString("1234hEllo"));
System.out.println(convertString("heLLO1234hEllo"));
System.out.println(convertString("$hello"));
System.out.println(convertString("$1234hEllo_TTHjjZ"));
}
catch (Exception e) {
e.printStackTrace();
}
}
private static String convertString(String string) {
String result = string;
final String regex1 = "^([^a-zA-Z]+)([a-zA-Z])([a-zA-Z]*)([^a-zA-Z].*)$";
final String regex2 = "^([a-zA-Z])([a-zA-Z]*)([^a-zA-Z].*)$";
final String regex3 = "^([^a-zA-Z]+)([a-zA-Z])([a-zA-Z]*)$";
final Pattern pattern1 = Pattern.compile(regex1, Pattern.MULTILINE);
final Pattern pattern2 = Pattern.compile(regex2, Pattern.MULTILINE);
final Pattern pattern3 = Pattern.compile(regex3, Pattern.MULTILINE);
Matcher matcher1 = pattern1.matcher(string);
Matcher matcher2 = pattern2.matcher(string);
Matcher matcher3 = pattern3.matcher(string);
if (matcher1.find()) {
result = matcher1.group(1) + matcher1.group(2).toUpperCase() + matcher1.group(3).toLowerCase() + matcher1.group(4);
}
else if (matcher2.find()) {
result = matcher2.group(1).toUpperCase() + matcher2.group(2).toLowerCase() + matcher2.group(3);
}
else if (matcher3.find()) {
result = matcher3.group(1) + matcher3.group(2).toUpperCase() + matcher3.group(3).toLowerCase();
}
return result;
}
The result is as expected:
1234_Hello
Hello Data
Hello data
1234Hello
Hello1234hEllo
$Hello
$1234Hello_TTHjjZ
I have a solution for you but it is not efficient:
public static String toCamelCase(String input) {
StringBuilder output = new StringBuilder();
for(int i = 0; i < input.length(); i++) {
if(i == 0) {
output.append(Character.toUpperCase(input.charAt(i)));
continue;
}
if(Character.isLetter(input.charAt(i))) {
if(Character.isLetter(input.charAt(i-1))) {
output.append(Character.toLowerCase(input.charAt(i)));
} else {
output.append(Character.toUpperCase(input.charAt(i)));
}
} else {
output.append(input.charAt(i));
}
}
return output.toString();
}
I'm trying to get all the strings from a javascript script, I created a code, but it's not catching all, it's skipping some
My Code
String Strings;
public String GetStrings(String str){
try{
String Str= str;
Strings = "";
while(true){
Pattern pattern = Pattern.compile("('|\")");
Matcher matcher = pattern.matcher(Str);
if(matcher.find()){
Pattern pattern1 = Pattern.compile("(" + matcher.group(1) + "[^" + matcher.group(1) + "]*" + matcher.group(1) + ")");
Matcher matcher1 = pattern1.matcher(Str);
if(matcher1.find()){
Strings += "|" + matcher1.group(1) + "|";
Str = Str.replace(matcher1.group(1)," ");
}
}else{
break;
}
}
}catch(Exception err){return err.toString(); }
return Strings;
}
Input
var A="&";var B="(";var D="[]";var X="'";var W='&';var Q='';var STR="'";var Q="'******'";var G="^";var F="...";var T='$';var wm = "()"
console.log(A + B + D + "^" + wm + '#');
Output
|"&"||"("||"[]"||"'"||'&'||''||"'******'"||"^"||"..."||'$'||"()"||'#'|
As you can see not captured all the strings, some did not appear, if anyone has any solution or can point the problem, please help me
You need to use following regex:
(\"(.*?)\")|(\'(.*?)\')
example:
public String getStrings(String str){
String regex = "(\\\"(.*?)\\\")|(\\'(.*?)\\')";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
String output = "";
while (matcher.find()){
output = output+"|"+matcher.group(0)+"|";
}
return output;
}
Output:
|"&"||"("||"[]"||"'"||'&'||''||"'"||"'******'"||"^"||"..."||'$'||"()"||"^"||'#'|
Regex Explanation
Input and expected output is not matching but according what i understood is
public String GetStrings(String str){
StringBuffer b = new StringBuffer();
for (int i = 0; i < str.length(); ++i) {
char ch = str.charAt(i);
if (Character.isWhitespace(ch))
b.append("\\s");
else if (Character.isDigit(ch))
b.append("\\d");
else if (Character.isUpperCase(ch))
b.append("A-Z");
else if (Character.isLowerCase(ch))
b.append("a-z");
}
b.append("||");
}
I am trying to separate these value into ID, FullName and Phone. I know we can split it by using java split function. But is there any other ways to separate it? Values:
1 Peater John 2522523254
10 Neal Tom 2522523254
11 Tom Jackson 2522523254
111 Jack Smith 2522523254
12 Brownson Black 2522523254
I tried to use substring method but it won't work properly.
String id = line.substring(0, 3);
If I do this then it will work till 4th line, but other won't work properly.
If it is fixed length you can use String.substring(). But you should also trim() the result before you try to convert it to numeric:
String idTxt=line.substring(0,4);
Long id=Long.parseLong(idTxt.trim());
String name=line.substring(5,25).trim(); // or whatever the size is of name column.
You can use regex and Pattern
Pattern pattern = Pattern.compile("(\\d*)\s*([\\w\\s]*)\\s*(\\d*)");
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
string id = matcher.group(0);
string name = matcher.group(1);
string phone = matcher.group(2);
}
package Generic;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main
{
public static void main(String[] args)
{
String txt=" 12 Brownson Black 2522523254";
String re1=".*?"; // Non-greedy match on filler
String re2="(\\d+)"; // Integer Number 1
String re3="(\\s+)"; // White Space 1
String re4="((?:[a-z][a-z]+))"; // Word 1
String re5="(\\s+)"; // White Space 2
String re6="((?:[a-z][a-z]+))"; // Word 2
String re7="(\\s+)"; // White Space 3
String re8="(\\d+)"; // Integer Number 2
Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{
int id = Integer.parseInt(m.group(1));
String name =m.group(3) + " ";
name = name+m.group(5);
long phone = Long.parseLong(m.group(7));
System.out.println(id);
System.out.println(name);
System.out.println(phone);
}
}
}
What about this:
int first_space;
int last_space;
first_space = my_string.indexOf(' ');
last_space = my_string.lastIndexOf(' ');
if ((first_space > 0) && (last_space > first_space))
{
long id;
String full_name;
String phone;
id = Long.parseLong(my_string.substring(0, first_space));
full_name = my_string.substring(first_space + 1, last_space);
phone = my_string.substring(last_space + 1);
}
Use a regexp:
private static final Pattern RE = Pattern.compile(
"^\\s*(\\d+)\\s+(\\S+(?: \\S+)*)\\s+(\\d+)\\s*$");
Matcher matcher = RE.matcher(s);
if (matcher.matches()) {
System.out.println("ID: " + matcher.group(1));
System.out.println("FullName: " + matcher.group(2));
System.out.println("Phone: " + matcher.group(3));
}
You can use a StringTokenizer for this. You won't have to worry about amount of spaces and/or tabs before or after your values, and no need for complex regex expressions:
String line = " 1 Peater John\t2522523254 ";
StringTokenizer st = new StringTokenizer(line, " \t");
String id = "";
String name = "";
String phone = "";
// The first token is your id, you can parse it to an int if you like or need it
if(st.hasMoreTokens()) {
id = st.nextToken();
}
// Loop over the remaining tokens
while(st.hasMoreTokens()) {
String token = st.nextToken();
// As long a there are other tokens, you're processing the name
if(st.hasMoreTokens()) {
if(name.length() > 0) {
name = name + " ";
}
name = name + token;
}
// If there are no more tokens, you've reached the phone number
else {
phone = token;
}
}
System.out.println(id);
System.out.println(name);
System.out.println(phone);
so i have a xml string that looks like this:
<CONFIG><Setting1><o1>44</o1><o2>1.0E-4</o2><o3>955</o3><o4>1.5E-4</o4><o5>Surname</o5></setting1>....</CONFIG>
How would i go about converting every float in a string from scientific-notion to the decimal-notation?
Edit: To clarify, im not looking to convert only a single float value from scientific to decimal nation. The String is read from a xml file that i serialized from a pojo, so all of the float values in the String would need to be converted. Sadly the XML-Framework i used (SimpleXML) only represents floats in scientific notation.
UPDATE:
Tried finding the float values with RegEx, it works. "found" will be the new converted decimal. How would i go about replacing each of the the found pattern with the "found"-String?
public static void ScientificToDecimal(String text){
String found;
Pattern pattern = Pattern.compile("\\d+[.]\\d+E[+-]\\d");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
found = new BigDecimal(matcher.group()).toPlainString();
Log.i("Converted: ", matcher.group() + " to " + found);
}
}
UPDATE2: Works good enough for me.
public static String scientificToDecimal(String text){
String replacementText = "";
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile("\\d+[.]\\d+E[+-]\\d");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
replacementText = new BigDecimal(matcher.group()).toPlainString();
matcher.appendReplacement(sb,replacementText);
Log.i("Converted: ", matcher.group() + " to " + replacementText);
}
matcher.appendTail(sb);
return sb.toString();
}
Think about those pattern >1.0E-4< or >1.5E-4< and RegEx and String replacement and so on.
Use XMLPullParser (consult the guide) to get the double values, then convert using the technique described here, or here, potentially use your regex.
Just to enhance to handle the following scenarios
a) 1.0E-4
b) 1.0E4
c) 1.0E+4
public static String scientificToDecimal(String text){
String out = "";
boolean found = false;
String replacementText = "";
StringBuffer sb = new StringBuffer();
/*
* 5.0E4
*/
Pattern pattern = Pattern.compile("\\d+[.]\\d+E\\d");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
replacementText = new BigDecimal(matcher.group()).toPlainString();
matcher.appendReplacement(sb,replacementText);
found = true;
// System.out.println("Converted: " + matcher.group() + " to " + replacementText);
}
if ( found )
{
matcher.appendTail(sb);
out = sb.toString();
return out;
}
/*
* 5.0E-4
*/
pattern = Pattern.compile("\\d+[.]\\d+E[-+]\\d");
matcher = pattern.matcher(text);
while(matcher.find()){
replacementText = new BigDecimal(matcher.group()).toPlainString();
matcher.appendReplacement(sb,replacementText);
// System.out.println("Converted: " + matcher.group() + " to " + replacementText);
}
matcher.appendTail(sb);
out = sb.toString();
return out;
}
I have a string
String str = "(varA=0.1;varB=0.2;varC<0.3;varD>=0.4)<?0.1>(varA=1.1;varB=1.2;varC<1.3;varD>=1.4)";
and I want to split it into
(varA=0.1;varB=0.2;varC<0.3;varD>=0.4)
<?0.1>
(varA=1.1;varB=1.2;varC<1.3;varD>=1.4)
I have tried
String[] parts = str.split("(?<=>)|(?=<)");
But it didn't work.
Any suggestion? Thanks!
But it pops error
You can use:
String[] parts = str.split("(?<=\\))(?=<)|(?<=>)(?=\\()");
RegEx Demo
Output:
(varA=0.1;varB=0.2;varC<0.3;varD>=0.4)
<?0.1>
(varA=1.1;varB=1.2;varC<1.3;varD>=1.4)
Try
public static void main(String[] args) {
String line = "(varA=0.1;varB=0.2;varC<0.3;varD>=0.4)<?0.1>(varA=1.1;varB=1.2;varC<1.3;varD>=1.4)";
String pattern = "(\\(.*\\))(<.*?>)(.*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
System.out.println("Found value: " + m.group(3) );
}
}