Reputation: 31
I'm attempting to take an input file that looks like so:
SomeElement1|SomeElementWithDifferentLength2|SomeElement3|AnElementI'llIgnore4|Something5| Element1|DifferentLength2|Element3|AnElementI'llIgnore4|Element5|
And format it into columns with specific spacing. I'm very new to java and come from a C++ background. Anyways I was looking into establishing a pattern with regex and then looking for matches. I'm not sure what the syntax in regex to indentify a specific character (a pipe "|") so I can determine the pattern.
Does this seem like a good approach, and if so what would the syntax for Pattern.compile("Regex syntax") be and how would I determine matches with it?
Upvotes: 1
Views: 120
Reputation: 272257
Why not simply use String.split() ? e.g.
String[] res = line.split("\\|");
(note that the split()
method takes a regexp, so you have to escape the pipe).
You can then iterate through the resultant array and use String.format() to output with padding/formatting etc.
Upvotes: 1
Reputation: 29814
Do not reinvent the wheel. Use Apache Commons Lang and check StringUtils.
I assume you want fixed length columns; the library provides leftPad() or rightPad() methods plus a heap of other String manipulations.
In short:
String.split()
StringUtils
Upvotes: 0
Reputation: 146
If you can read the file contents as a string then you can do any of the following:
directly replace the "|" with a certain number of spaces: stringContents.replaceAll("\\|", " ")
split the string at "|": stringContents.split("\\|");
Remember, the "|" is a special character in regular expressions and is interpreted as an "OR" operation, which is why it needs to be escaped with a backwards slash (i.e. two backwards slashes)
Upvotes: 0
Reputation: 27474
I think the simplest thing would be:
Turn each line into an array with String.split("|")
Create an array of ints with the length of each column
Create a StringBuilder to hold the reformatted line
Loop through the array from line 1, padding each string to the length from the array in line 2, and then appending it to the StringBuilder.
If it's possible for fields to have an embedded pipe and thus you have to have some mechanism for escaping them, then instead of split() you'd have to write code to parse the columns yourself. I suppose you could write a Regex for that but I think that would be more work than just looping through characters looking for pipes.
Upvotes: 3