开发者

Isolate SQL field using regex

开发者 https://www.devze.com 2022-12-25 04:17 出处:网络
I\'m trying to isolate a specific field in a SQL dump file so I can edit it but I\'m not having any luck.

I'm trying to isolate a specific field in a SQL dump file so I can edit it but I'm not having any luck.

The regex I'm using is:

^(?:(?:'[^\r\n']*'|[^,\r\n]*),){6}('[^\r\n']*'|[^,\r\n]*)

Which is supposed to grab the seventh field and place it inside reference 1.

The trouble开发者_如何学C is that this is stumbling when ever it finds a comma inside a text field and counts the partial match as the allowable matches.

Eg. (1, 'Title', 1, 3, '2006-09-29', 'Commas, the bane of my regex', 'This is the target', 2, 4) matches " the bane of my regex'" instead of "'This is the target'".


It might be easier to load the SQL into a temp database and then do a SELECT to get the data in that field.


Do you have control over the dump file, or are they historic or outside of your control?

If you can choose a better delimeter, comma really is a terrible choice.


[^,\r\n]*, matches

'Commas,

I suggest [^,\r\n']*, instead.


I think you will have more luck if you make the regex more specific. I havent tested this but I believe this should work.

Also as Paul suggests you might try a different delimiter to make this easier.

Enjoy!

\d{1,4}(,){1}('){1}[a-zA-Z0-9,]+('){1}\d{1,4}(,){1}\d{1,4}(,){1}('){1}[0-9-]+('){1}(,){1}('){1}[a-zA-Z0-9,]+('){1}(,){1}('){1}[a-zA-Z0-9,]+('){1}(,){1}\d{1,4}(,){1}\d{1,4}(\r\n){1}


Doh!

My fields weren't just split with a comma. They were split with a comma followed by a space.

Correct RegEx is

^(?:(?:'[^\r\n']*'|[^,\r\n]*), ){6}('[^\r\n']*'|[^,\r\n]*)

Now it works.

Sorry to waste you time with this one. It was Beta's response that got me thinking as it was the second alternation in play for all fields. The extra space forced it to use this option rather than the option enclosed within quotes.

0

精彩评论

暂无评论...
验证码 换一张
取 消