Reputation:
I have a file with variable columns that I'm converting to a SQL import file, or csv is you will.
The file is setup as follows:
Account Source Source2 Source3 Source4 Source5
'39','39'
'41','41'
'67','67'
'286','286'
'299','299','2312210299'
'307','307'
'341','341'
'349','349'
'351','351'
'359','359'
'362','362'
'363','363'
'378','378'
'511','511','6218','2197360511'
I'm trying to use Regex to find the number of occurrences of each string per line so I can pad with NULL
, but just can't seem to get it right.
If I search for '[0-9]\*'
, it finds each instance.
If I search for ['[0-9]\*']\*$
, it will find the last instance in each line.
If I use '[0-9]\*'{2}$
, it doesn't find any with 2 occurrences per line, and from what I'm finding online, that should be the syntax.
Any help with this would be much appreciated.
Upvotes: 1
Views: 1852
Reputation: 5615
Let's get the reasoning behind why the things you tried didn't work first.
If I search for
'[0-9]*'
, it finds each instance
Yes that's exactly what it is supposed to do; I'm sure you already figured that out.
If I search for
['[0-9]*']*$
, it will find the last instance in each line
I'm not sure where you're going with this one, square bracketts within square bracketts is a terrible idea.
If I search for
['[0-9]*']*$
, it will find the last instance in each line. If I use'[0-9]*'{2}$
, it doesn't find any with 2 occurrences per line, and from what I'm finding online, that should be the syntax
See the reason this didn't work is because '[0-9]*'{2}$
will match 2 multidigit numbers (or no digits at all because you're using asterisk) back to back. But the numbers in your dataset are separated by a ,
you HAVE to take that into account. Regex is an explicit tool.
What you'd do instead is this - ^('[0-9]*',?){2}$
Notice the presence, of an optional ,
in the capture group. The above regex will match lines with 2 numbers within quotes (or no numbers at all but just a ''
since we're using asterisk) separated by a ,
.
Here's the demo
I'm not exactly sure whether this was what you wanted, if not, just comment it down and I'll edit the answer to provide more stuff.
Upvotes: 2