How can i split 100.26kg to System.out.println as
100.26
kg
I tried doing by using ("\d+") but was not successful.
String[] num = myTextCount.split("\\d+");
The 100.26kg may vary according to user input. It could be 100.26kg, 100g, 100 pounds or 100litre. If there is any way to split the number and alphabet, it will be very helpful
myTextCount.split("[\\.\\d]+") will give you [, kg] which contain the second part, and then use #indexOf() to find the first part.
Try look-around regex,
String[] num = myTextCount.split("(?<=\\d)(?=[a-z])|(?<=[a-z])(?=\\d)");
If you want to split case-insensitive with number than use (?i)
String[] num = myTextCount.split("(?i)(?<=\\d)(?=[a-z])|(?<=[a-z])(?=\\d)");
import java.util.Scanner;
public class Sample {
public static void main(String[] args) {
String inputs[] = { "100.26kg", "100g", "100 pounds", "100litre"};
String weight, unit;
for(String input : inputs){
Scanner scan = new Scanner(input);
weight = scan.findInLine("\\d+(\\.\\d+)?");
unit = scan.next();//scan.next("\\w+");
System.out.println(weight);
System.out.println(unit);
}
}
}
Do not use Split, just use the String.IndexOf() function to deal with it.
Matcher m = Pattern.compile("^([0-9.]+)\\s*([a-zA-Z]+)$").matcher("");
String inputs[] = {"100.26kg", "1 pound", "98gallons" };
for(String input: inputs)
{
if ( m.reset(input).find() )
{
System.out.printf("amount=[%s] unit=[%s]\n", m.group(1), m.group(2) );
}
}
yields:
amount=[100.26] unit=[kg]
amount=[1] unit=[pound]
amount=[98] unit=[gallons]
Try this:
private static final Pattern VALID_PATTERN = Pattern.compile("[0-9]+|[A-Z]+");
private List<String> parse(String toParse) {
List<String> chunks = new LinkedList<String>();
Matcher matcher = VALID_PATTERN.matcher(toParse);
while (matcher.find()) {
chunks.add( matcher.group() );
}
return chunks;
}
This solution is quite modular as well.
Related
I want to split this string (1,0) and get result 1 and 0 i have tried this code:
String str ="(1,0)";
String parts[]= str.split("(,)");
System.out.println(parts[0]);
System.out.println(parts[1]);
But i got this :
(1
0)
Here's an efficient way you can isolate all your digits using the Regex Tools and put them into an ArrayList for easy usage. It doesn't use the .split() method, but it is efficient.
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public static void main(String[] args) {
String str = "(1,0)";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
ArrayList<Integer> vals = new ArrayList<>();
while(m.find())
vals.add(Integer.parseInt(m.group()));
System.out.println(vals);
}
A simple solution would be as follows:
public class Main {
public static void main(String[] args) {
String str = "(1,0)";
String parts[] = str.replace("(", "").replace(")", "").split(",");
System.out.println(parts[0]);
System.out.println(parts[1]);
}
}
Output:
1
0
If you split on (, the first value in the returned array will be "".
I'd recommend using regex directly, e.g.
String input = "(1,0)";
Matcher m = Pattern.compile("\\(([^,\\)]+),([^,\\)]+)\\)").matcher(input);
if (! m.matches())
throw new IllegalArgumentException("Invalid input: " + input);
System.out.println(m.group(1));
System.out.println(m.group(2));
Of course, if you insist on using split(), it can be done like this:
String input = "(1,0)";
String[] parts = input.split("[(,)]");
if (parts.length != 3 || ! parts[0].isEmpty())
throw new IllegalArgumentException("Invalid input: " + input);
System.out.println(parts[1]);
System.out.println(parts[2]);
If you know how to use regex, go for that. (personally I prefer to use string manipulation here because it's really easier) If not, learn how to use it or do something like this:
String input = "(64,128)";
String[] numbers = input.substring(1, input.length() - 1).split(",");
Try this, assuming your format is consistent.
String str = "(1,0)";
String[] tokens = str.substring(1,str.length()-1).split(",");
System.out.println(Arrays.toString(tokens));
Prints
[1, 0]
or if printed separately
1
0
I need to split a string in Java (first remove whitespaces between quotes and then split at whitespaces.)
"abc test=\"x y z\" magic=\" hello \" hola"
becomes:
firstly:
"abc test=\"xyz\" magic=\"hello\" hola"
and then:
abc
test="xyz"
magic="hello"
hola
Scenario :
I am getting a string something like above from input and I want to break it into parts as above. One way to approach was first remove the spaces between quotes and then split at spaces. Also string before quotes complicates it. Second one was split at spaces but not if inside quote and then remove spaces from individual split. I tried capturing quotes with "\"([^\"]+)\"" but I'm not able to capture just the spaces inside quotes. I tried some more but no luck.
We can do this using a formal pattern matcher. The secret sauce of the answer below is to use the not-much-used Matcher#appendReplacement method. We pause at each match, and then append a custom replacement of anything appearing inside two pairs of quotes. The custom method removeSpaces() strips all whitespace from each quoted term.
public static String removeSpaces(String input) {
return input.replaceAll("\\s+", "");
}
String input = "abc test=\"x y z\" magic=\" hello \" hola";
Pattern p = Pattern.compile("\"(.*?)\"");
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer("");
while (m.find()) {
m.appendReplacement(sb, "\"" + removeSpaces(m.group(1)) + "\"");
}
m.appendTail(sb);
String[] parts = sb.toString().split("\\s+");
for (String part : parts) {
System.out.println(part);
}
abc
test="xyz"
magic="hello"
hola
Demo
The big caveat here, as the above comments hinted at, is that we are really using a regex engine as a rudimentary parser. To see where my solution would fail fast, just remove one of the quotes by accident from a quoted term. But, if you are sure you input is well formed as you have showed us, this answer might work for you.
I wanted to mention the java 9's Matcher.replaceAll lambda extension:
// Find quoted strings and remove there whitespace:
s = Pattern.compile("\"[^\"]*\"").matcher(s)
.replaceAll(mr -> mr.group().replaceAll("\\s", ""));
// Turn the remaining whitespace in a comma and brace all.
s = '{' + s.trim().replaceAll("\\s+", ", ") + '}';
Probably the other answer is better but still I have written it so I will post it here ;) It takes a different approach
public static void main(String[] args) {
String test="abc test=\"x y z\" magic=\" hello \" hola";
Pattern pattern = Pattern.compile("([^\\\"]+=\\\"[^\\\"]+\\\" )");
Matcher matcher = pattern.matcher(test);
int lastIndex=0;
while(matcher.find()) {
String[] parts=matcher.group(0).trim().split("=");
boolean newLine=false;
for (String string : parts[0].split("\\s+")) {
if(newLine)
System.out.println();
newLine=true;
System.out.print(string);
}
System.out.println("="+parts[1].replaceAll("\\s",""));
lastIndex=matcher.end();
}
System.out.println(test.substring(lastIndex).trim());
}
Result is
abc
test="xyz"
magic="hello"
hola
It sounds like you want to write a basic parser/Tokenizer. My bet is that after you make something that can deal with pretty printing in this structure, you will soon want to start validating that there arn't any mis-matching "'s.
But in essence, you have a few stages for this particular problem, and Java has a built in tokenizer that can prove useful.
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
public class Q50151376{
private static class Whitespace{
Whitespace(){ }
#Override
public String toString() {
return "\n";
}
}
private static class QuotedString {
public final String string;
QuotedString(String string) {
this.string = "\"" + string.trim() + "\"";
}
#Override
public String toString() {
return string;
}
}
public static void main(String[] args) {
String test = "abc test=\"x y z\" magic=\" hello \" hola";
StringTokenizer tokenizer = new StringTokenizer(test, "\"");
boolean inQuotes = false;
List<Object> out = new LinkedList<>();
while (tokenizer.hasMoreTokens()) {
final String token = tokenizer.nextToken();
if (inQuotes) {
out.add(new QuotedString(token));
} else {
out.addAll(TokenizeWhitespace(token));
}
inQuotes = !inQuotes;
}
System.out.println(joinAsStrings(out));
}
private static String joinAsStrings(List<Object> out) {
return out.stream()
.map(Object::toString)
.collect(Collectors.joining());
}
public static List<Object> TokenizeWhitespace(String in){
List<Object> out = new LinkedList<>();
StringTokenizer tokenizer = new StringTokenizer(in, " ", true);
boolean ignoreWhitespace = false;
while (tokenizer.hasMoreTokens()){
String token = tokenizer.nextToken();
boolean whitespace = token.equals(" ");
if(!whitespace){
out.add(token);
ignoreWhitespace = false;
} else if(!ignoreWhitespace) {
out.add(new Whitespace());
ignoreWhitespace = true;
}
}
return out;
}
}
from the String value want to getting word before and after the <in>
String ref = "application<in>rid and test<in>efd";
int result = ref.indexOf("<in>");
int result1 = ref.lastIndexOf("<in>");
String firstWord = ref.substring(0, result);
String[] wor = ref.split("<in>");
for (int i = 0; i < wor.length; i++) {
System.out.println(wor[i]);
}
}
my Expected Output
String[] output ={application,rid,test,efd}
i tried with 2 Option first one IndexOf but if the String have more than two <in>i 'm not getting my expected output
Second One splitits also not getting with my expected Output
please suggest best option to getting the word(before and after <in>)
You could use an expression like so: \b([^ ]+?)<in>([^ ]+?)\b (example here). This should match the string prior and after the <in> tag and place them in two groups.
Thus, given this:
String ref = "application<in>rid and test<in>efd";
Pattern p = Pattern.compile("\\b([^ ]+?)<in>([^ ]+?)\\b");
Matcher m = p.matcher(ref);
while(m.find())
System.out.println("Prior: " + m.group(1) + " After: " + m.group(2));
Yields:
Prior: application After: rid
Prior: test After: efd
Alternatively using split:
String[] phrases = ref.split("\\s+");
for(String s : phrases)
if(s.contains("<in>"))
{
String[] split = s.split("<in>");
for(String t : split)
System.out.println(t);
}
Yields:
application
rid
test
efd
Regex is your friend :)
public static void main(String args[]) throws Exception {
String ref = "application<in>rid and test<in>efd";
Pattern p = Pattern.compile("\\w+(?=<in>)|(?<=<in>)\\w+");
Matcher m = p.matcher(ref);
while (m.find()) {
System.out.println(m.group());
}
}
O/P :
application
rid
test
efd
No doubt matching what you need using Pattern/Matcher API is simpler for tis problem.
However if you're looking for a short and quick String#split solution then you can consider:
String ref = "application<in>rid and test<in>efd";
String[] toks = ref.split("<in>|\\s+.*?(?=\\b\\w+<in>)");
Output:
application
rid
test
efd
RegEx Demo
This regex splits on <in> or a pattern that matches a space followed by 0 more chars followed by a word and <in>.
You can also try the below code, it is quite simple
class StringReplace1
{
public static void main(String args[])
{
String ref = "application<in>rid and test<in>efd";
System.out.println((ref.replaceAll("<in>", " ")).replaceAll(" and "," "));
}
}
i have written a code in java as given under
public class sstring
{
public static void main(String[] args)
{
String s="a=(b+c); string st='hello adeel';";
String[] ss=s.split("\\b");
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
and the output of this code is
a
=(
b
+
c
);
string
st
='
hello
adeel
';
what should i do in order to split =( or ); etc in two separate elements rather than single elements. in this array. i.e. my output may look as
a
=
(
b
+
c
)
;
string
st
=
'
hello
adeel
'
;
is it possible ?
This matches with every find either a word \\w+ (small w) or a non-word character \\W (capital W).
It is an unaccepted answer of can split string method of java return the array with the delimiters as well of the above comment of #RohitJain.
public String[] getParts(String s) {
List<String> parts = new ArrayList<String>();
Pattern pattern = Pattern.compile("(\\w+|\\W)");
Matcher m = pattern.matcher(s);
while (m.find()) {
parts.add(m.group());
}
return parts.toArray(new String[parts.size()]);
}
Use this code there..
Pattern pattern = Pattern.compile("(\\w+|\\W)");
Matcher m = pattern.matcher("a=(b+c); string st='hello adeel';");
while (m.find()) {
System.out.println(m.group());
}
I have the following String:
mac1: 00:11:22:33:44:55
mac2: 66:77:88:99:00:11
model: PI-504
first_name: any_name
device_type: baseboard
serial_number: 668778542298745210
And I want to extract all values into an array. How to do it with Java?
public String[] getvaluesIntoStringArray(String str) {
....
}
You can use regular expressions:
private static final Pattern PATTERN = Pattern.compile(".*?:(.*)");
public static String[] getvaluesIntoStringArray(String str) {
Matcher matcher = PATTERN.matcher(str);
List<String> values = new ArrayList<String>();
while (matcher.find())
values.add(matcher.group(1).trim());
return values.toArray(new String[values.size()]);
}
If you want to split new lines then I think this should do it
public String[] getvaluesIntoStringArray(String str) {
return str.split("\\r?\\n");
}
use this regex (?<=:\s)(.+$)
if your regex engine does not suppirt lookbehind use this regex (:\s)(.+$) matches will be in group 2
p.s.: use regex with regexoption MultyLine
String str = "mac1: 00:11:22:33:44:55
mac2: 66:77:88:99:00:11
model: PI-504
first_name: any_name
device_type: baseboard
serial_number: 668778542298745210";
String[] tempArr = str.split("\n");
this tempArr should contain the values as mac1: 00:11:22:33:44:55 , mac2: 66:77:88:99:00:11,model: PI-504,first_name: any_name,device_type: baseboard,serial_number: 668778542298745210
Please try
String[] result = str.split("\n");
Here code:
public static void main(String[] args) {
List<String> result = new ArrayList<String>();
String str = "mac1: 00:11:22:33:44:55\n" +
"mac2: 66:77:88:99:00:11\n" +
"model: PI-504\n" +
"first_name: any_name\n" +
"device_type: baseboard\n" +
"serial_number: 668778542298745210";
Pattern pattern = Pattern.compile("^.*?:(.*)$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(str);
boolean find = matcher.find();
while(find) {
result.add(matcher.group(1));
find = matcher.find();
}
System.out.print(result);
}