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;
}
Related
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);
I am reading a text file which contains movie titles, year, language etc.
I am trying to grab those attributes.
Suppose some string are like this :
String s = "A Fatal Inversion" (1992)"
String d = "(aka "Verhngnisvolles Erbe" (1992)) (Germany)"
String f = "\"#Yaprava\" (2013) "
String g = "(aka \"Love Heritage\" (2002)) (International: English title)"
How can i grab title, year, country if specified, what sort of title if specified from this?
I am not very good at using regex and patterns, but I don't know how to find what sort of attribute it is when they are not specified. I am doing this because I am trying to generate xml from a textfile. I have the dtd for it but im not sure I need it to use it in this case.
Edit: Here is what i have tried.
String pattern;
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m;
Pattern number = Pattern.compile("\\d+");
Matcher num;
m = p.matcher(s);
num = number.matcher(s);
if(m.find()){
System.out.println(m.group(1));
}
if(num.find()){
System.out.println(num.group(0));
}
I suggest you extract the year first as this seems fairly consistent. Then I'd extract the country (if present) and the rest I'll assume is the title.
For extracting the countries I'd recommend you hardcode a regex pattern with the names of known countries. It might take some iterating to determine what these are as they seem to be pretty inconsistent.
This code is a bit ugly (but then so is the data!):
public class Extraction {
public final String original;
public String year = "";
public String title = "";
public String country = "";
private String remaining;
public Extraction(String s) {
this.original = s;
this.remaining = s;
extractBracketedYear();
extractBracketedCountry();
this.title = remaining;
}
private void extractBracketedYear() {
Matcher matcher = Pattern.compile(" ?\\(([0-9]+)\\) ?").matcher(remaining);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
this.year = matcher.group(1);
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
remaining = sb.toString();
}
private void extractBracketedCountry() {
Matcher matcher = Pattern.compile("\\((Germany|International: English.*?)\\)").matcher(remaining);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
this.country = matcher.group(1);
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
remaining = sb.toString();
}
public static void main(String... args) {
for (String s : new String[] {
"A Fatal Inversion (1992)",
"(aka \"Verhngnisvolles Erbe\" (1992)) (Germany)",
"\"#Yaprava\" (2013) ",
"(aka \"Love Heritage\" (2002)) (International: English title)"}) {
Extraction extraction = new Extraction(s);
System.out.println("title = " + extraction.title);
System.out.println("country = " + extraction.country);
System.out.println("year = " + extraction.year);
System.out.println();
}
}
}
Produces:
title = A Fatal Inversion
country =
year = 1992
title = (aka "Verhngnisvolles Erbe")
country = Germany
year = 1992
title = "#Yaprava"
country =
year = 2013
title = (aka "Love Heritage")
country = International: English title
year = 2002
Once you've got this data, you can manipulate it further (e.g. "International: English title" -> "England").
I want to find and count all the occurrences of the words unit, device, method, module in every line of the text file separately. That's what I've done, but I don't know how to use multiple patterns and how to count the occurrence of every word in the line separately? Now it counts only occurrences of all words together for every line. Thank you in advance!
private void countPaterns() throws IOException {
Pattern nom = Pattern.compile("unit|device|method|module|material|process|system");
String str = null;
BufferedReader r = new BufferedReader(new FileReader("D:/test/test1.txt"));
while ((str = r.readLine()) != null) {
Matcher matcher = nom.matcher(str);
int countnomen = 0;
while (matcher.find()) {
countnomen++;
}
//intList.add(countnomen);
System.out.println(countnomen + " davon ist das Wort System");
}
r.close();
//return intList;
}
Better to use a word boundary and use a map to keep counts of each matched keyword.
Pattern nom = Pattern.compile("\\b(unit|device|method|module|material|process|system)\\b");
String str = null;
BufferedReader r = new BufferedReader(new FileReader("D:/test/test1.txt"));
Map<String, Integer> counts = new HashMap<>();
while ((str = r.readLine()) != null) {
Matcher matcher = nom.matcher(str);
while (matcher.find()) {
String key = matcher.group(1);
int c = 0;
if (counts.containsKey(key))
c = counts.get(key);
counts.put(key, c+1)
}
}
r.close();
System.out.println(counts);
Here's a Java 9 (and above) solution:
public static void main(String[] args) {
List<String> expressions = List.of("(good)", "(bad)");
String phrase = " good bad bad good good bad bad bad";
for (String regex : expressions) {
Pattern gPattern = Pattern.compile(regex);
Matcher matcher = gPattern.matcher(phrase);
long count = matcher.results().count();
System.out.println("Pattern \"" + regex + "\" appears " + count + (count == 1 ? " time" : " times"));
}
}
Outputs
Pattern "(good)" appears 3 times
Pattern "(bad)" appears 5 times
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