This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 4 years ago.
Currently attempting to split a large string field into 3 smaller fields. The string delimited by a "/". Example String:
0123/ABCD1234/EFGH909883432212
At the moment I have managed to pull the middle section out using the following expression inside a variable:
$F{String}.split("/" ,5)[1].trim()
To be perfectly honest I am not sure how it works as I do not know what the 5 and 1 are for (which is probably what I need to know to get the other two sections)
After calling method spit an array is created holding substrings which are delimited by "/" in the original string. The trim removes any trailing spaces.
Number 5 resembles optional parameter.
An integer that specifies the number of splits, items after the split limit will not be included in the array.
Related
This question already has answers here:
Regex to split a CSV
(18 answers)
Closed 1 year ago.
I am trying to split data from a .csv file, however, some of the fields/columns also contain commas in between just like this
ABCKS,"ASK,ED","SDR,ED",2022-07-11,8011.0
cvbgb,"hfhvnf,rgr","dthd,chdf",2022-07-11,111.9
ABCKS,"ASK,ED","SDR,ED",2022-07-11,8011.0
hence, the .split(",") string method would create additional fields into the data.
I have tried
if (aLine.contains("\"|,|\"")){
String newString = aLine.replaceAll("\"|,|\"","|_|").replaceAll("\"", "");
aList = Arrays.asList(newString.split(",", -1));
}
It does not seem to work.
As Tim said above, a proper CSV reader would probably be better but a simple solution could be something like this: ,(?![A-Za-z]+"). Where you select every comma that is not followed by letters and a quotation. This satisfies your sample data but if there are edge cases it can easily break.
This question already has answers here:
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 2 years ago.
I have this regular expression that I wrote, which extracts text in between tags like "#<string to extract>":
"#<(.+?)>"
I need to make sure that the length of the string I'm extracting is 6 and my current solution is checking the length of the string that I extracted with an if statement. I would like to replace this with a regex instead. How could I modify "#<(.+?)>" to make sure it is 6 characters when extracted?
You can use curly braces to specify the length of the match.
#<(.{6})>
This question already has answers here:
Regex match empty lines
(2 answers)
Closed 4 years ago.
I have a String that looks as follows:
This is line number 1.
[space][space][space][space]\n
[space]\n
This is line number 2.
where every [space] represents a blank space and \n represents a new line.
What I would like to do is to split this string into two strings, one that has "This is line number 1." and the other that contains "This is line number 2." In other words split the string on every two empty lines regardless of whether they contain spaces or not.
What I tried to do:
System.out.println(myString.split("^[ ]{0,}\\n")[0]);
But the above prints the whole string.
UPDATE
Other things I have tried that also print the whole string and don't seem to work:
System.out.println(myString.split("(^[ ]{0,}\\n){2,}")[0]);
These all print the whole string as well. Any ideas?
Simply enable the multiline flag in your pattern like this.
myString.split("(?m)^[ ]{0,}\n")
The ?m character adds a multiline flag you can pass without using Java's Regex class.
This should work, not sure if you get the extra split caused by the first line.
I'm just briefly looking through things on a break at work so perhaps I haven't read the question thoroughly enough, but have you tried:
System.out.println(parsedText.split("^[ ]{0,}\n\n")[0]);
Seems like you are not completely skipping two lines in your code. Once again might be wrong but worth a shot!
This question already has answers here:
How to filter string for unwanted characters using regex?
(7 answers)
Closed 5 years ago.
Assume you had a string "ACcwerwervwvrwBq^2424 /.* DffGZ..'B". How would you only keep certain characters like A,B,C,D and remove the rest?
string.replaceAll seems to work if I know what characters to remove, but I want to remove all characters except A,B,C,D. Putting every character in there except those 4 seems pretty tedious, what the easier way?
I want the output in the above case to be "ACBDB".
You would use regex something like:
str.replaceAll("[^ABCD]", "");
Should do it
You just need a proper regex:
s.replaceAll("[^ABCD]", "")
This question already has answers here:
How to extract numbers from a string and get an array of ints?
(13 answers)
Closed 8 years ago.
As i'm really bad in REGEX, I'm looking for some help here. a string can be of the following two formats:
1. id://3
2. id://3/1
How can i check if one of the two formats is available (because there's e.g. id://next – id + strings – as well) and how can i extract in the first case the "3" and the second case the "3" and "1" in separate variables? the numbers can be anything (3,1 is just an example).
thanks in advance!
You can use this regex:
\bid:\/.*?\/(\d+)\b(?!\/)
RegEx Demo