1

I had a question (answered excellently) here: Python parse dataframe element

Unfortunately, my data source has other conditions which need handled.

Current pattern is

pattern = r'([^\(]+)(\(([^,]*),(.*)\))?'

trans_field_attr = df['Data Type'].str.extract(pattern, expand=True).iloc[:, [0, 2, 3]]

This handles the (precision,scale) version perfectly e.g NUMBER(22,4). Unfortunately it does not select any values in brackets where there is only a single value.

For example:

0        VARCHAR2(1)
1        VARCHAR2(1)
2        VARCHAR2(1)
3        VARCHAR2(1)
4        VARCHAR2(1)
5            DATE(7)
6            DATE(7)
7            DATE(7)
8            DATE(7)
9        VARCHAR2(1)
10           DATE(7)
11       VARCHAR2(3)
12       VARCHAR2(3)
13               NaN
14       VARCHAR2(3)
15      NUMBER(22,4)

How could the pattern be improved to pickup single values as well?

Apologies but I really struggled to take it further from piRSquared's answer...

0

2 Answers 2

1

Add a non-capturing group for the second number and the comma and then add a ? zero or one token after it, like below.

([^\(]+)(\(([^,]*)(?:,(.*))?\))?
                  (?:     )? <= this part means that the comma and everything following it
                                is optional, alike to the ? token at the very end.
Sign up to request clarification or add additional context in comments.

1 Comment

just a note that it is worth looking at Oz123's answer for if you want something that is a lot more scalable. (say you want more than 2 values in between brackets.)
1

If you are just trying to extract the number between the brackets you can use a much simple version:

In [2]: rgx=re.compile("\w+\((?P<num>\d*\,*\d*)")
In [5]: m=rgx.match("VARCHAR(22,22)")
In [10]: m.groupdict()
Out[10]: {'num': '22,22'}

In [16]: m=rgx.match("VARCHAR(22)")
In [17]: m.groupdict()['num']
Out[17]: '22'

1 Comment

might be worth mentioning that this would require additional parsing (something like .groupdict()['num]).split(',')) since it seems that OP wants to split each element up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.