@Override
  public Object doDeserialize(Writable blob) throws SerDeException {

    if (inputPattern == null) {
      throw new SerDeException(
          "This table does not have serde property \"input.regex\"!");
    }
    Text rowText = (Text) blob;

    Matcher m = inputPattern.matcher(rowText.toString());

    // If do not match, ignore the line, return a row with all nulls.
    if (!m.matches()) {
      unmatchedRows++;
      if (unmatchedRows >= nextUnmatchedRows) {
        nextUnmatchedRows = getNextNumberToDisplay(nextUnmatchedRows);
        // Report the row
        LOG.warn("{} unmatched rows are found: {}", unmatchedRows, rowText);
      }
      return null;
    }

    // Otherwise, return the row.
    for (int c = 0; c < numColumns; c++) {
      try {
        row.set(c, m.group(c + 1));
      } catch (RuntimeException e) {
        partialMatchedRows++;
        if (partialMatchedRows >= nextPartialMatchedRows) {
          nextPartialMatchedRows = getNextNumberToDisplay(nextPartialMatchedRows);
          // Report the row
          LOG.warn("" + partialMatchedRows
              + " partially unmatched rows are found, " + " cannot find group "
              + c + ": " + rowText);
        }
        row.set(c, null);
      }
    }
    return row;
  }
RegexSerDe uses regular expression (regex) to serialize/deserialize.

It can deserialize the data using regex and extracts groups as columns. It
can also serialize the row object using a format string.

In deserialization stage, if a row does not match the regex, then all columns
in the row will be NULL. If a row matches the regex but has less than
expected groups, the missing groups will be NULL. If a row matches the regex
but has more than expected groups, the additional groups are just ignored.

In serialization stage, it uses java string formatter to format the columns
into a row. If the output type of the column in a query is not a string, it
will be automatically converted to String by Hive.

For the format of the format String, please refer to link: http
://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

NOTE: Obviously, all columns have to be strings. Users can use
"CAST(a AS INT)" to convert columns to other types.

NOTE: This implementation is using String, and javaStringObjectInspector. A
more efficient implementation should use UTF-8 encoded Text and
writableStringObjectInspector. We should switch to that when we have a UTF-8
based Regex library.
contrib/src/java/org/apache/hadoop/hive/contrib/serde2/RegexSerDe.java