Question: I've a Java class which are exactly the same but under 2 different namespaces and cannot be modified. I require to convert both of those classes into an internal pojo representation of mine. Is there a way I could do in a generic manner without copying the same code.

For ex:

```
com.namespace.first;

public ClassA {
    private String a;
    private String b;
}

```

&

```
com.namespace.second;

public ClassA {
    private String a;
    private String b;
}

```

Now I've a Pojo which needs to be filled in by values coming from `ClassA`:

```
com.namespace.third

public ClassC {
   private String a;
   private String b;
   private String d;
   private String e;
}

```

One option is:

```
public ClassC convert(com.namespace.first.ClassA classA) {
   return new ClassC(classA.getA(), classA.getB(), "c", "d");
}

public ClassC convert(com.namespace.second.ClassA classA) {
   return new ClassC(classA.getA(), classA.getB(), "c", "d");
}

```

Is there a different route I could use instead of having 2 convert methods which does exactly the same thing (While this example is simple, in my case it is a lot bigger)?

Answer: 
Try with [`extract`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.extract.html#pandas-series-str-extract) and a regex to have the second capture group be the value after the optional `-`:

```
df[['B', 'C']] = df['A'].str.extract(r"(\d+$|\d+(?=\s*-))?(?:\s*-\s*)?(.+)?")

```

```none
                          A       B                   C
0       00000-UNITED STATES   00000       UNITED STATES
1             01000-ALABAMA   01000             ALABAMA
2  01001-Autauga County, AL   01001  Autauga County, AL
3  01003-Baldwin County, AL   01003  Baldwin County, AL
4        Barbour County, AL     NaN  Barbour County, AL
5                     10234   10234                 NaN
6                32 Alabama     NaN          32 Alabama
7            432423 - state  432423               state

```

---

Complete Code:

```
import pandas as pd

df = pd.DataFrame({
    'A': ['00000-UNITED STATES', '01000-ALABAMA',
          '01001-Autauga County, AL', '01003-Baldwin County, AL',
          'Barbour County, AL', '10234', '32 Alabama', '432423 - state']
})

df[['B', 'C']] = df['A'].str.extract(r"(\d+$|\d+(?=\s*-))?(?:\s*-\s*)?(.+)?")

```
You could create two functions to extract the desired elements from COLUMN A and assign to COLUMN B and COLUMN C:

```
def get_col_b(item):
    if '-' in item:
        return item.split('-')[0]
    else:
        return ''

def get_col_c(item):
    if '-' in item:
        return item.split('-')[1]
    else:
        return item

```

Create the columns and then drop COLUMN A:

```
df['COLUMN B'] = df['COLUMN A'].apply(get_col_b)
df['COLUMN C'] = df['COLUMN A'].apply(get_col_c)
cols = ['COLUMN B', 'COLUMN C']
df = df[cols]

```

[![enter image description here](https://i.stack.imgur.com/Eshcm.png)](https://i.stack.imgur.com/Eshcm.png)