public static @Nullable TableReference toTableReference(String fullTableId) {
    // Try parsing the format:
    // "projects/{project_id}/datasets/{dataset_id}/tables/{table_id}"
    Matcher m = TABLE_RESOURCE_PATTERN.matcher(fullTableId);
    if (m.matches()) {
      return new TableReference()
          .setProjectId(m.group("PROJECT"))
          .setDatasetId(m.group("DATASET"))
          .setTableId(m.group("TABLE"));
    }

    // If that failed, try the format:
    // "{project_id}:{dataset_id}.{table_id}" or
    // "{project_id}.{dataset_id}.{table_id}"
    m = SIMPLE_TABLE_PATTERN.matcher(fullTableId);
    if (m.matches()) {
      return new TableReference()
          .setProjectId(m.group("PROJECT"))
          .setDatasetId(m.group("DATASET"))
          .setTableId(m.group("TABLE"));
    }
    return null;
  }
@Test
  public void testToTableReference() {
    {
      TableReference tr =
          BigQueryUtils.toTableReference("projects/myproject/datasets/mydataset/tables/mytable");
      assertEquals("myproject", tr.getProjectId());
      assertEquals("mydataset", tr.getDatasetId());
      assertEquals("mytable", tr.getTableId());
    }

    {
      // Test colon(":") after project format
      TableReference tr = BigQueryUtils.toTableReference("myprojectwithcolon:mydataset.mytable");
      assertEquals("myprojectwithcolon", tr.getProjectId());
      assertEquals("mydataset", tr.getDatasetId());
      assertEquals("mytable", tr.getTableId());
    }

    {
      // Test dot(".") after project format
      TableReference tr = BigQueryUtils.toTableReference("myprojectwithdot.mydataset.mytable");
      assertEquals("myprojectwithdot", tr.getProjectId());
      assertEquals("mydataset", tr.getDatasetId());
      assertEquals("mytable", tr.getTableId());
    }

    {
      // Test project that contains a dot and colon
      TableReference tr = BigQueryUtils.toTableReference("project.with:domain.mydataset.mytable");
      assertEquals("project.with:domain", tr.getProjectId());
      assertEquals("mydataset", tr.getDatasetId());
      assertEquals("mytable", tr.getTableId());
    }

    // Invalid scenarios
    assertNull(BigQueryUtils.toTableReference(""));
    assertNull(BigQueryUtils.toTableReference(":."));
    assertNull(BigQueryUtils.toTableReference(".."));
    assertNull(BigQueryUtils.toTableReference("myproject"));
    assertNull(BigQueryUtils.toTableReference("myproject:"));
    assertNull(BigQueryUtils.toTableReference("myproject."));
    assertNull(BigQueryUtils.toTableReference("myproject:mydataset"));
    assertNull(BigQueryUtils.toTableReference("myproject:mydataset."));
    assertNull(BigQueryUtils.toTableReference("myproject:mydataset.mytable."));
    assertNull(BigQueryUtils.toTableReference("myproject:mydataset:mytable:"));
    assertNull(BigQueryUtils.toTableReference("myproject:my dataset:mytable:"));
    assertNull(BigQueryUtils.toTableReference(".invalidleadingdot.mydataset.mytable"));
    assertNull(BigQueryUtils.toTableReference("invalidtrailingdot.mydataset.mytable."));
    assertNull(BigQueryUtils.toTableReference(":invalidleadingcolon.mydataset.mytable"));
    assertNull(BigQueryUtils.toTableReference("invalidtrailingcolon.mydataset.mytable:"));
    assertNull(BigQueryUtils.toTableReference("projectendswithhyphen-.mydataset.mytable"));
    assertNull(
        BigQueryUtils.toTableReference(
            "projectnamegoesbeyondthe30characterlimit.mydataset.mytable"));

    assertNull(
        BigQueryUtils.toTableReference("/projects/extraslash/datasets/mydataset/tables/mytable"));
    assertNull(
        BigQueryUtils.toTableReference("projects//extraslash/datasets/mydataset/tables/mytable"));
    assertNull(
        BigQueryUtils.toTableReference("projects/extraslash//datasets/mydataset/tables/mytable"));
    assertNull(
        BigQueryUtils.toTableReference("projects/extraslash/datasets//mydataset/tables/mytable"));
    assertNull(
        BigQueryUtils.toTableReference("projects/extraslash/datasets/mydataset//tables/mytable"));
    assertNull(
        BigQueryUtils.toTableReference("projects/extraslash/datasets/mydataset/tables//mytable"));
    assertNull(
        BigQueryUtils.toTableReference("projects/extraslash/datasets/mydataset/tables/mytable/"));

    assertNull(BigQueryUtils.toTableReference("projects/myproject/datasets/mydataset/tables//"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject/datasets//tables/mytable/"));
    assertNull(BigQueryUtils.toTableReference("projects//datasets/mydataset/tables/mytable/"));
    assertNull(BigQueryUtils.toTableReference("projects//datasets//tables//"));

    assertNull(
        BigQueryUtils.toTableReference("projects/myproject/datasets/mydataset/tables/mytable/"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject/datasets/mydataset/tables/"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject/datasets/mydataset/tables"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject/datasets/mydataset/"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject/datasets/mydataset"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject/datasets/"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject/datasets"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject/"));
    assertNull(BigQueryUtils.toTableReference("projects/myproject"));
    assertNull(BigQueryUtils.toTableReference("projects/"));
    assertNull(BigQueryUtils.toTableReference("projects"));
  }