InSpace
InSpace

Reputation: 31

Pattern and Matches: Formatting a text file

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

Answers (5)

Brian Agnew
Brian Agnew

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

Bruno Grieder
Bruno Grieder

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:

  1. split using String.split()
  2. loop over the array and reformat using StringUtils

Upvotes: 0

Vivek Thakyal
Vivek Thakyal

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

Zarathustra
Zarathustra

Reputation: 2943

You could use the String.split(String regex) Method.

Upvotes: 0

Jay
Jay

Reputation: 27474

I think the simplest thing would be:

  1. Turn each line into an array with String.split("|")

  2. Create an array of ints with the length of each column

  3. Create a StringBuilder to hold the reformatted line

  4. 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

Related Questions