title: How do I ensure the format for saving and parsing string representations of Objects correlate properly
body: <p>I am making a small boardgame program which needs to persist the state of the board to a file, and later read from the file and re-create the board. </p>
  
  <p>I am delegating this functionality to the class shown below. I would like to implement this such that the save format of a square of the board along with it's co-ordinates are captured in the SQUARE_FORMAT constant, and the regex for reading that same information is captured in the LOAD_REGEX constant.  Both should co-relate in code and also be able to visually decipher (by that I mean that a person should be able to clearly see that they co-relate to the same data)</p>
  
  <p>Is there an idiom or pattern for doing this in Java code ?</p>
  
  <pre><code>public class BoardPersistenceUtility {
  
      private final String SQUARE_SAVE_FORMAT = "";
      private fial String LOAD_REGEX = "";
  
      public void save(PrintWriter writer, Board board) {
  
      }
  
      public Board load(BufferedReader reader) {
          // Implement
          return null;
      }
  }
  </code></pre>
  
  <p><strong>Update 1:</strong></p>
  
  <p>On reading my question again, I guess it might be a bit confusing, about what exactly I am looking for. I am specifically looking for the right way to represent SQUARE_SAVE_FORMAT so that it clearly co-relates with the regex LOAD_REGEX. </p>
  
  <p>SQUARE_SAVE_FORMAT would ideally be a String which uses special characters/variables that will be replaced with actual values and the result will be saved to a file. LOAD_REGEX is the corresponding regex that will be used to read contents from the file. The regex will use capturing groups so I can re-create the original object from the values I get from the capturing groups. </p>
  
  <p>My question is, what are the idioms around creating such pairs of Strings - one of them a format string to be used for saving data, and the other a regex to be used while reading that data.</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>On thinking a bit more, I think I have been able to clarify my question a bit better.</p>
  
  <p>If you look at both the Strings, SQUARE_SAVE_FORMAT is a format string which will be used in String.format() to create the text for a square on the board, which will be saved in the file. The constant SQUARE_LOAD_REGEX is a regex which will be used to read the line and capture relevant parts into named groups, so I can re-create the original object. (sorry if my regex is slightly incorrect... I quickly wrote something, but I need to refresh some regex principles to ensure that this is indeed what I need)</p>
  
  <p>If you look at both these Strings visually, it is difficult to co-relate them together. Perhaps it is because we do not have any named variables in a Java format String. The best we can do is to specify %i where i is the index of the argument. </p>
  
  <p>I would like to understand if there is any idiom or pattern to represent such pairs of Strings, where one is used for formatting some data to text and the other is used to read the same text and parse it's parts. </p>
  
  <pre><code>public class BoardPersistenceUtility {
  
      private final String SQUARE_SAVE_FORMAT = 
                    "%d,%d:%b-%s";
      private final String SQUARE_LOAD_REGEX = 
                    "^(?&lt;row&gt;\d*),(?&lt;col&gt;\d*):(?&lt;mine&gt;true|false)-(?&lt;status&gt;\w)$";
  
      public void save(PrintWriter writer, Board board) {
  
      }
  
      public Board load(BufferedReader reader) {
          // Implement
          return null;
      }
  }
  </code></pre>