I have this string:
fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862
I want to use a loop to split this string into an array at the comma [,] example:
[0]fname lname
[1]GTA V: 120 : 00000
[2]Minecraft : 20 : 10
[3]Assassin’s Creed IV : 90 : 800
[4]Payday 2 : 190 : 2001
[5]Wolfenstein TNO : 25 : 80
[6]FarCry 4 : 55 : 862
Then I want to use another loop to split this further at : into another array example
[0]fname lname
[1]GTA V
[2]120
[3]00000
[4]Minecraft
[5]20
[6]10
....
Is there a better way of doing this?
currently I have:
List<String> lines = new ArrayList<String>();
while (scan.hasNextLine())
{
lines.add(scan.nextLine());
}
//converts the list array to string array
String[] scanarray = lines.toArray(new String[0]);
//converts the string array into one large string
String str_array = Arrays.toString(scanarray);
String[] arraysplit;
arraysplit = str_array.split("\\s*:\\s*");
for (int i=0; i<arraysplit.length; i++)
{
arraysplit[i] = arraysplit[i].trim();
// ^^^^^^^^^^^^ has values with spaces
System.out.println(scanarray[i]);
}
EDIT:
Currently my program creates 3 identical arrays, containing the example you can see in the second block of code above.
You can use the split method from String class with multiple delimiters
public static void main(String[] args) {
String myOriginalString = " fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862";
// | is the regex OR operator
String[] splited = myOriginalString.split(",|:");
for(String s : splited)
System.out.println(s.trim());
}
You can achieve it what you are looking for with REGEX, just put what all thing you get separated with string split method.
I tried below code locally and it is pretty much same what you are looking for.
public class StackSol1 {
public static void main(String[] args) {
String str = "fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 :80, FarCry 4 : 55 : 862";
String delimiters = "\\s+|,\\s*|\\:\\s*";
// analyzing the string
String[] tokensVal = str.split(delimiters);
// prints the number of tokens
System.out.println("Count of tokens = " + tokensVal.length);
String finalStr="";
for (String token : tokensVal) {
finalStr = finalStr+"\n"+token;
}
System.out.println(finalStr);
}
}
How about using split with regex? e.g.
String aa = "fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862";
String [] a = aa.split("[,:]");
Related
I have a collection in mongodb with below data:
collection name: runState
runId: 1
startTime:2020-09-16T20:56:06.598+00:00
endTime:2020-09-16T20:57:09.196+00:00
product_action: org_rhel_oracle_install
Task: completed
ranBy:David
runId: 2
startTime:2021-01-11T20:56:06.598+00:00
endTime:2021-01-11T20:56:09.196+00:00
product_action: org_rhel_oracle_install
Task: completed
ranBy:John
runId: 2
startTime:2021-01-27T20:56:06.598+00:00
endTime:2021-01-27T20:56:09.196+00:00
product_action: org_rhel_oracle_install
Task: completed
ranBy:John
runId: 3
startTime:2021-01-11T20:56:06.598+00:00
endTime:2021-01-11T20:57:09.196+00:00
product_action: org_rhel_postgres_install
Task: completed
ranBy:John
runId: 4
startTime:2021-02-09T20:56:06.598+00:00
endTime:2021-02-09T20:57:09.196+00:00
product_action: org_rhel_oracle_install
Task: completed
ranBy:John
runId: 5
startTime:2021-02-09T20:56:06.598+00:00
endTime:2021-02-09T20:57:09.196+00:00
product_action: org_rhel_postgres_install
Task: completed
ranBy:John
runId: 6
startTime:2021-09-09T20:56:06.598+00:00
endTime:2021-09-09T20:57:09.196+00:00
product_action: org_rhel_postgres_install
Task: completed
ranBy:John
runId: 7
startTime:2022-01-09T20:56:06.598+00:00
endTime:2022-01-09T20:57:09.196+00:00
product_action: org_rhel_oracle_install
Task: completed
ranBy:David
runId: 8
startTime:2022-01-10T20:56:06.598+00:00
endTime:2022-01-10T20:57:09.196+00:00
product_action: org_rhel_oracle_install
Task: failed
ranBy:David
I want the output as count for last 12 months (Jan 2021 to Jan 2022) for each products where task is completed( product is gettable from product_action)
Output should be in below format:
{
"_id" : "postgres",
completed: [
{
"month" : "FEB-2021",
"count" : 1
},
{
"month" : "SEP-2021",
"count" : 1
},
{
"month" : "JAN-2021",
"count" : 1
}
]
},
{
"_id" : "oracle",
"completed" : [
{
"month" : "FEB-2021",
"count" : 1
},
{
"month" : "JAN-2021",
"count" : 2
}
]
}
I have started with below, but not sure how to get count for month wise like above.
{"product_action":{$regex:"postgres|oracle"},"Task":"completed"}
As this is new to me, can someone help me with mongo DB query to get the result and also code to acheive this in Java springboot?
Java code I tried using aggregation, but this is not yielding the result I want.
Aggregation agg = Aggregation.newAggregation(
Aggregation.project("endTime","Task","product_action").and(DateOperators.Month.monthOf("endTime")).as("month"),
Aggregation.match(Criteria.where("product_action").regex("postgres|oracle").and("Task").is("completed")
.and("endTime").gte(parseDate("2021-02-01"))),
Aggregation.group("month","Task").count().as("count")
);
Try this on for size:
db.foo.aggregate([
// Get easy stuff out way. Filter for the desired date range and only
// those items that are complete:
{$match: {$and: [
{"endTime":{$gte:new ISODate("2021-01-01")}},
{"endTime":{$lt:new ISODate("2022-01-01")}},
{"Task":"completed"}
]} }
// Now group by product and date expressed as month-year. The product
// is embedded in the field value so there are a few approaches to digging
// it out. Here, we split on underscore and take the [2] item.
,{$group: {_id: {
p: {$arrayElemAt:[{$split:["$product_action","_"]},2]},
d: {$dateToString: {date: "$endTime", format: "%m-%Y"}}
},
n: {$sum: 1}
}}
// The OP seeks to make the date component nested inside the product
// instead of having it as a two-part grouping. We will "regroup" and
// create an array. This is slightly different than the format indicated
// by the OP but values as keys (e.g. "Jan-2021: 2") is in general a
// poor idea so instead we construct an array of proper name:value pairs.
,{$group: {_id: '$_id.p',
completed: {$push: {d: '$_id.d', n: '$n'}}
}}
]);
which yields
{
"_id" : "postgres",
"completed" : [
{
"d" : "02-2021",
"n" : 1
},
{
"d" : "09-2021",
"n" : 1
},
{
"d" : "01-2021",
"n" : 1
}
]
}
{
"_id" : "oracle",
"completed" : [
{
"d" : "02-2021",
"n" : 1
},
{
"d" : "01-2021",
"n" : 2
}
]
}
UPDATED
It has come up before that the $dateToString function does not have a format argument to produce the 3 letter abbreviation for a month e.g. JAN (or a long form e.g. January for that matter). Sorting still works with 01-2021,02-2021,04-2021 vs. JAN-2021,FEB-2021,APR-2021 but if such output is really desired directly from the DB instead of post-processing in the client-side code, then the second group is replaced by a $sort and $group as follows:
// Ensure the NN-YYYY dates are going in increasing order. The product
// component _id.p does not matter here -- only the dates have to be
// increasing. NOTE: This is OPTIONAL with respect to changing
// NN-YYYY into MON-YYYY but almost always the follow on question is
// how to get the completed list in date order...
,{$sort: {'_id.d':1}}
// Regroup as before but index the NN part of NN-YYYY into an
// array of 3 letter abbrevs, then reconstruct the string with the
// dash and the year component. Remember: the order of the _id
// in the doc stream coming out of $group is not deterministic
// but the array created by $push will preserve the order in
// which it was pushed -- which is the date-ascending sorted order
// from the prior stage.
,{$group: {_id: '$_id.p',
completed: {$push: {
d: {$concat: [
{$arrayElemAt:[ ['JAN','FEB','MAR',
'APR','MAY','JUN',
'JUL','AUG','SEP',
'OCT','NOV','DEC'],
// minus 1 to adjust for zero-based array:
{$subtract:[{$toInt: {$substr:['$_id.d',0,2]}},1]}
]},
"-",
{$substr:['$_id.d',3,4]}
]},
n: '$n'}}
}}
which yields:
{
"_id" : "postgres",
"completed" : [
{
"d" : "JAN-2021",
"n" : 1
},
{
"d" : "FEB-2021",
"n" : 1
},
{
"d" : "SEP-2021",
"n" : 1
}
]
}
{
"_id" : "oracle",
"completed" : [
{
"d" : "JAN-2021",
"n" : 2
},
{
"d" : "FEB-2021",
"n" : 1
}
]
}
As for converting this to Java, there are several approaches but unless a great deal of programmatic control is required, then capturing the query as "relaxed JSON" (quotes not required around keys) in a string in Java and calling Document.parse() seems to be the easiest way. A full example including helper functions and the appropriate Java drivers calls can be found here: https://moschetti.org/rants/mongoaggcvt.html but the gist of it is:
private static class StageHelper {
private StringBuilder txt;
public StageHelper() {
this.txt = new StringBuilder();
}
public void add(String expr, Object ... subs) {
expr.replace("'", "\""); // This is the helpful part.
if(subs.length > 0) {
expr = String.format(expr, subs); // this too
}
txt.append(expr);
}
public Document fetch() {
Document b = Document.parse(txt.toString());
return b;
}
}
private List<Document> makePipeline() {
List<Document> pipeline = new ArrayList<Document>();
StageHelper s = new StageHelper();
s.add("{$match: {$and: [ ");
// Note use of EJSON here plus string substitution of dates:
s.add(" {endTime:{$gte: {$date: '%s'}} }", "2021-01-01");
s.add(" {endTime:{$lt: {$date: '%s'}} }", "2022-01-01");
s.add(" {Task:'completed'} ");
s.add("]} } ");
pipeline.add(s.fetch());
s = new StageHelper();
s.add("{$group: {_id: { ");
s.add(" p: {$arrayElemAt:[{$split:['$product_action','_']},2]}, ");
s.add(" d: {$dateToString: {date: '$endTime', 'format': '%m-%Y'}} ");
s.add(" }, ");
s.add(" n: {$sum: 1} ");
s.add("}} ");
pipeline.add(s.fetch());
s = new StageHelper();
s.add("{$sort: {'_id.d':1}} ");
pipeline.add(s.fetch());
s = new StageHelper();
s.add("{$group: {_id: '$_id.p', ");
s.add(" completed: {$push: { ");
s.add(" d: {$concat: [ ");
s.add(" {$arrayElemAt:[ ['JAN','FEB','MAR', ");
s.add(" 'APR','MAY','JUN', ");
s.add(" 'JUL','AUG','SEP', ");
s.add(" 'OCT','NOV','DEC'], ");
s.add(" {$subtract:[{$toInt: {$substr:['$_id.d',0,2]}},1]} ");
s.add(" ]}, ");
s.add(" '-', ");
s.add(" {$substr:['$_id.d',3,4]} ");
s.add(" ]}, ");
s.add(" n: '$n'}} ");
s.add(" }} ");
pipeline.add(s.fetch());
return pipeline;
}
...
import com.mongodb.client.MongoCursor;
import com.mongodb.client.AggregateIterable;
AggregateIterable<Document> output = coll.aggregate(pipeline);
MongoCursor<Document> iterator = output.iterator();
while (iterator.hasNext()) {
Document doc = iterator.next();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to replace or rename a string/tags/keys in-between string in java.
Before jump into my string please notice that there should Bd and Bg and counts two times. Rename tags for the spouse section. its can remove duplicate and all key/tag is unique
My String is
Name : Abul Bashar Fakir , cl : Knit Fabrics Manufacturer , In : 16 June 2003 , Hpc : President , Hpd : Assistant Governor , Bd : April 6 , Bg : B + , Wd : September 27 , Sp : Hossneara Begum , Bd : March 8 , Bg : 0+ , Ma : 98 North Chasara Narayanganj - 1400 , Tel : ( O ) 01617 008 519 ( R ) 763 2407 , Mob : 01715 393 127 01824 554 123 , Email : basharfokir66#gmail.com
Please notice that Here Bd and Bg count 2 times. one is for the main person and the other is for his/her spouse and Tag is Sp. I want to rename all Same tags for Sp with S character append. Example: Bd and Bg To SBd, SBg In-between Sp tag and Ma tag. it will create a unique tag for husband and spouse.
My Targeted String
Name : Abul Bashar Fakir , cl : Knit Fabrics Manufacturer , In : 16 June 2003 , Hpc : President , Hpd : Assistant Governor , Bd : April 6 , Bg : B + , Wd : September 27 , Sp : Hossneara Begum , SBd : March 8 , SBg : 0+ , Ma : 98 North Chasara Narayanganj - 1400 , Tel : ( O ) 01617 008 519 ( R ) 763 2407 , Mob : 01715 393 127 01824 554 123 , Email : basharfokir66#gmail.com
Note: It must return as a String.
If you need to create a unique tag for husband and spouse then
Use Java String replaceFirst() method and rename husband tags with a specific name.
String str = "Name : Abul Bashar Fakir , cl : Knit Fabrics Manufacturer , In : 16 June 2003 , Hpc : President , Hpd : Assistant Governor , Bd : April 6 , Bg : B + , Wd : September 27 , Sp : Hossneara Begum , Bd : March 8 , Bg : 0+ , Ma : 98 North Chasara Narayanganj - 1400 , Tel : ( O ) 01617 008 519 ( R ) 763 2407 , Mob : 01715 393 127 01824 554 123 , Email : basharfokir66#gmail.com
";
//Only Replace first 'Bd' with 'HBd'
String str1 = str.replaceFirst("Bd", "HBd");
You can use replace all from the String class. myString.replaceAll("bd", "Sbd")
I want to split string by colon and make it into key and value pairs. For that in convert function, I took a line and made it into key and value pairs.
I want to convert the line variable. The problem is in the convert function. It's not converting as expected. It must show as expected Output shown below. I don't want to split the code as a new line for every key-value pair. I want to do it by a comma in the same line.
Code is
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Splitter;
public class stack {
public static Map<String, String> convert(String str, String SplitByWhat) {
String[] tokens = str.split(SplitByWhat);
Map<String, String> map = new HashMap<>();
for (int i = 0; i < tokens.length - 1;) {
map.put(tokens[i++], tokens[i++]);
}
return map;
}
public static void main(String[] args) {
String line = "Name : Md . Abdur Rab , CI : Marketing Petroleum Products , In : May 2013 , Hpc : President , Bd : April 14 , Bg : B + , Wd : October 10 , Sp : Quazi Manzida Rab , Bd : August 22 , Bg : B + , Ma : 689 Bara Moghbazar Dhaka - 1217 , Tel : ( 0 ) 935 1684 835 0818 , Mob : 01819 247 932 , Email : mdarab123#gmail.com";
Iterable<String> result = Splitter.on(" , ").trimResults().split(line);
for (String v : result) {
Map<String, String> response = convert(v, ":");
System.out.println(response);
}
}
}
Output is
{Name = Md . Abdur Rab}
{CI = Marketing Petroleum Products}
{In = May 2013}
{Hpc = President}
{Bd = April 14}
{Bg = B +}
{Wd = October 10}
{Sp = Quazi Manzida Rab}
{Bd = August 22}
{Bg = B +}
{Ma = 689 Bara Moghbazar Dhaka - 1217}
{Tel = ( 0 ) 935 1684 835 0818}
{Mob = 01819 247 932}
{Email = mdarab123#gmail.com}
Expected Output
{Name = Md . Abdur Rab, CI = Marketing Petroleum Products, In = May 2013, Hpc = President, Bd = April 14, Bg = B +, Wd = October 10, Sp = Quazi Manzida Rab, Bd = August 22, Bg = B +, Ma = 689 Bara Moghbazar Dhaka - 1217, Tel = ( 0 ) 935 1684 835 0818, Mob = 01819 247 932, Email = mdarab123#gmail.com}
You are creating a new map each time you call convert() and printing that out, so you are printing a map for each line separated by ','.
What you want to do is something along the lines of:
public static void main(String[] args) {
String line = ...
Iterable<String> result = Splitter.on(" , ").trimResults().split(line);
Map<String, String> response - new HashMap<>();
for (String v : result) {
Entry<String, String> entry = convert(v, ":");
response.put(entry.getKey(), entry.getValue());
}
System.out.println(response);
}
where convert() returns type Entry<String, String>.
See: https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html
Hello, I modified your code to print exactly what you wanted, so try this code:
public static Map<String, String> convert(String str, String SplitByWhat) {
String[] tokens = str.split(SplitByWhat);
Map<String, String> map = new HashMap<>();
for (int i = 0; i < tokens.length - 1;) {
map.put(tokens[i++], tokens[i++]);
}
return map;
}
public static void main(String[] args) {
String line = "Name : Md . Abdur Rab , CI : Marketing Petroleum Products , In : May 2013 , Hpc : President , Bd : April 14 , Bg : B + , Wd : October 10 , Sp : Quazi Manzida Rab , Bd : August 22 , Bg : B + , Ma : 689 Bara Moghbazar Dhaka - 1217 , Tel : ( 0 ) 935 1684 835 0818 , Mob : 01819 247 932 , Email : mdarab123#gmail.com";
Iterable<String> result = Splitter.on(" , ").trimResults().split(line);
String response = "";
for (String v : result) {
response += convert(v, ":");
}
response = response.replace("{","");
response = response.replace("}",", ");
response = "{" + response + "}";
System.out.println(response);
}
Have fun, I hope I could help you!
I have been picking my brain lately and can't seem to figure out how to pull the "text" from this string and replace the found pattern with those word(s).
Pattern searchPattern = Pattern.compile("\\[\\{(.+?)\\}\\]");
Matcher matcher = searchPattern.matcher(sb);
sb is the string that contains a few occurrences of these patterns that start with [{ and end with ]}.
[{ md : {o : "set", et : _LU.et.v.v }, d : {t : _LU.el.searchtype, l : _LU[_LU.el.searchtype].nfts.l, v : _LU[_LU.el.searchtype].nfts.v}}, { md : {o : "set", et : _LU.et.v.v }, d : {t : _LU.el.topicgroup, l : "Books", v : "ETBO"}}]
gets returned as
md : {o : "set", et : _LU.et.v.v }, d : {t : _LU.el.searchtype, l : _LU[_LU.el.searchtype].nfts.l, v : _LU[_LU.el.searchtype].nfts.v}}, { md : {o : "set", et : _LU.et.v.v }, d : {t : _LU.el.topicgroup, l : "Books", v : "ETBO"}
Notice the lack of [{ and }]. I manage to find the above pattern but how would I find the words set and Book and then replace the original found pattern with only those words. I can search the string if it contains a " via
while (matcher.find()) {
matcher.group(1).contains("\"");
but I really just need some ideas about how to go about doing this.
Is this what you are looking for (answer based on your first comment)?
its actually fairly large.. but goes along the lines of "hello my name is, etc, etc, etc, [{ md : {o : "set", et : _LU.et.v.v }, d : {t : _LU.el.searchtype, l : _LU[_LU.el.searchtype].nfts.l, v : _LU[_LU.el.searchtype].nfts.v}}, { md : {o : "set", et : _LU.et.v.v }, d : {t : _LU.el.topicgroup, l : "Books", v : "ETBO"}}] , some more text here, and some more" -> the [{ }] parts should be replaced with the text inside of them in this case set, books, etbo... resulting in a final string of "hello my name is, etc, etc, etc, set set Books ETBO , some more text here, and some more"
// text from your comment
String sb = "hello my name is, etc, etc, etc, [{ md : "
+ "{o : \"set\", et : _LU.et.v.v }, d : {t : "
+ "_LU.el.searchtype, l : _LU[_LU.el.searchtype].nfts.l, "
+ "v : _LU[_LU.el.searchtype].nfts.v}}, { md : {o : "
+ "\"set\", et : _LU.et.v.v }, d : {t : _LU.el.topicgroup, "
+ "l : \"Books\", v : \"ETBO\"}}] , "
+ "some more text here, and some more";
Pattern searchPattern = Pattern.compile("\\[\\{(.+?)\\}\\]");
Matcher matcher = searchPattern.matcher(sb);
// pattern that finds words between quotes
Pattern serchWordsInQuores = Pattern.compile("\"(.+?)\"");
// here I will collect words in quotes placed in [{ and }] and separate
// them with one space
StringBuilder words = new StringBuilder();
// buffer used while replacing [{ xxx }] part with words found in xxx
StringBuffer output = new StringBuffer();
while (matcher.find()) {// looking for [{ xxx }]
words.delete(0, words.length());
//now I search for words in quotes from [{ xxx }]
Matcher m = serchWordsInQuores.matcher(matcher.group());
while (m.find())
words.append(m.group(1)).append(" ");
matcher.appendReplacement(output, words.toString().trim());
//trim was used to remove last space
}
//we also need to append last part of String that wasn't used in matcher
matcher.appendTail(output);
System.out.println(output);
Output:
hello my name is, etc, etc, etc, set set Books ETBO , some more text here, and some more
OK, I think you need to do this in three passes, first time matching the section between the [{ }], and the second time going through the match doing the replace, and the third time replacing that match with the string you got from the second pass.
You already have a pattern for the first match, and you'd just use it again for the third match, when you replace it with the result of the second pass.
For the second pass, you're going to need to replaceAll on the first match. Something like this:
Pattern searchPattern = Pattern.compile("\\[\\{(.+?)\\}\\]");
Matcher matcher = searchPattern.matcher(sb);
while ( matcher.find() )
{
matcher.replaceFirst(matcher.group(1).replaceAll("[^\"]*\"([^\"]*)\"", "$1"));
}
The first pass is done by matcher.find(). The next one is done by matcher.group().replaceAll(), which is then passed into matcher.replaceFirst() for the third pass. The third pass is a little weird: it replaces the first example of the [{ }]. However, since we're starting from the beginning and moving forward, that will be the one we just found, and we won't match it again because it will get replaced by a non-matching string. The docs recommend resetting the matcher after replaceFirst(), but I think it will be safe here because it will continue from after that replacement, which is exactly what we want.
I would point out that this is not particularly efficient. I think that you would be better off doing more of this manually rather than with regular expressions.
LATEST REVISION
An Example on how to loop over a string with multiple boundaries and replacing at each level
public static String replace(CharSequence rawText, String oldWord, String newWord, String regex) {
Pattern patt = Pattern.compile(regex);
Matcher m = patt.matcher(rawText);
StringBuffer sb = new StringBuffer(rawText.length());
while (m.find()) {
String text = m.group(1);
if(oldWord == null || oldWord.isEmpty()) {
m.appendReplacement(sb, Matcher.quoteReplacement(newWord));
} else {
if(text.matches(oldWord)) {
m.appendReplacement(sb, Matcher.quoteReplacement(newWord));
}
}
}
m.appendTail(sb);
return sb.toString();
}
public static void main(String[] args) throws Exception {
String rawText = "[{MY NAME IS \"NAME\"}]";
rawText += " bla bla bla [{I LIVE IN \"SOME RANDOM CITY\" WHERE THE PIZZA IS GREAT!}]";
rawText += " bla bla etc etc [{I LOVE \"A HOBBY\"}]";
System.out.println(rawText);
Pattern searchPattern = Pattern.compile("\\[\\{(.+?)\\}\\]");
Matcher matcherBoundary = searchPattern.matcher(rawText);
List<String> replacement = new ArrayList<String>();
replacement.add("BOB");
replacement.add("LOS ANGELES");
replacement.add("PUPPIES");
int counter = 0;
while (matcherBoundary.find()) {
String result = Test.replace(matcherBoundary.group(1), null, replacement.get(counter), "\"([^\"]*)\"");
System.out.println(result);
counter++;
}
}
The output I get is:
**Raw Text**
[{MY NAME IS "NAME"}] bla bla bla [{I LIVE IN "SOME RANDOM CITY" WHERE THE PIZZA IS GREAT!}] bla bla etc etc [{I LOVE "A HOBBY"}]
**In Every Loop**
MY NAME IS BOB
I LIVE IN LOS ANGELES WHERE THE PIZZA IS GREAT!
I LOVE PUPPIES
I want to split a number of strings similar to name: john, id: 20, dest: toledo, from: seattle, date_time: [2/8/12 15:48:01:837 MST] into only these tokens:
john
20
toledo
seattle
[2/8/12 15:48:01:837 MST]
I'm doing this
String delims = "(name|id|dest|from|date_time)?[:,\\s]+";
String line = "name: john, id: 20, dest: toledo, from: seattle, date_time: [2/8/12 15:48:01:837 MST]";
String[] lineTokens = line.split(delims, 5);
for (String t : lineTokens)
{
// for debugging
System.out.println (t);
// other processing I want to do
}
but every even element in lineTokens turns out to be either empty or just whitespace. Each odd element in lineTokens is what I want, i.e. lineTokens[0] is "", lineTokens[1] is "john", lineTokens[2] is "", lineTokens[3] is "20", etc. Can anyone explain what I'm doing wrong?
The problem is that your regex is not matching , id: as a whole, it is matching , as one and then id: as a 2nd match. Between these two matches you have an empty string. You need to modify it to match the whole thing. Something like this:
String delims = "(, )?(name|id|dest|from|date_time)?[:\\s]+";
http://ideone.com/Qgs8y
Why not a little less complicated regex solution.
String str = "name: john, id: 20, dest: toledo, from: seattle, date_time: [2/8/12 15:48:01:837 MST]";
String[] expr = str.split(", ");
for(String e : expr)
System.out.println(e.split(": ")[1]);
Output =
john
20
toledo
seattle
[2/8/12 15:48:01:837 MST]
I made some changes to your code:
String delims = "(name|id|dest|from|date_time)[:,\\s]+";
String line = "name: john, id: 20, dest: toledo, from: seattle, date_time: [2/8/12 15:48:01:837 MST]";
String[] lineTokens = line.split(delims);
for (String t : lineTokens)
{
// for debugging
System.out.println (t);
// other processing I want to do
}
also you should ignore the first element in lineTokens, since it's the capturing from the beginning of the line till "name:...."