title: Java Regex for matching hexadecimal numbers in a file
body: <p>So I'm reading in a file (like java program &lt; trace.dat) which looks something like this:</p>
  
  <pre><code>58
  68
  58
  68
  40
  c
  40
  48
  FA
  </code></pre>
  
  <p>If I'm lucky but more often it has several whitespace characters before and after each line.</p>
  
  <p>These are hexadecimal addresses that I'm parsing and I basically need to make sure that I can get the line using a scanner, buffered reader... whatever and make sure I can then convert the hexadecimal to an integer. This is what I have so far:</p>
  
  <pre><code>Scanner scanner = new Scanner(System.in);
  int address;
  String binary;
  Pattern pattern = Pattern.compile("^\\s*[0-9A-Fa-f]*\\s*$", Pattern.CASE_INSENSITIVE);
  while(scanner.hasNextLine()) {
      address = Integer.parseInt(scanner.next(pattern), 16);
      binary = Integer.toBinaryString(address);
      //Do lots of other stuff here
  }
  //DO MORE STUFF HERE...
  </code></pre>
  
  <p>So I've traced all my errors to parsing input and stuff so I guess I'm just trying to figure out what regex or approach I need to get this working the way I want.</p>