Python Regular expression -matching repeating pattern in one shot





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20))


From this query String i want to get each of the column details (ie, col1 decimal(10,2) and col2 char(20) etc ) as a list in a single group(#anynumber) call.
currently what i'am doing is given below :



columnResult = re.match(r"^CREATE(sw+s*){1,}((w+)s(S+),s*(w+)s(S+)",line)
if columnResult == None:
pass
else:
print("column = ",columnResult.group(2),",",columnResult.group(4))
print("type = ",columnResult.group(3),", ",columnResult.group(5))


Instead of repeating (w+)s(S+) for each column ,how can i match all repeating column details in one shot?
Please help










share|improve this question


















  • 1





    what is your exact expected outcome for the example string?

    – Jean-François Fabre
    Nov 15 '18 at 16:10











  • no matter how many column details are there i want them to be printed on screen.

    – c0der
    Nov 15 '18 at 16:13











  • Actually i want to print them in the following way :- column name = col1 ,type=decimal, length=10 and precision=2

    – c0der
    Nov 15 '18 at 16:14


















0















CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20))


From this query String i want to get each of the column details (ie, col1 decimal(10,2) and col2 char(20) etc ) as a list in a single group(#anynumber) call.
currently what i'am doing is given below :



columnResult = re.match(r"^CREATE(sw+s*){1,}((w+)s(S+),s*(w+)s(S+)",line)
if columnResult == None:
pass
else:
print("column = ",columnResult.group(2),",",columnResult.group(4))
print("type = ",columnResult.group(3),", ",columnResult.group(5))


Instead of repeating (w+)s(S+) for each column ,how can i match all repeating column details in one shot?
Please help










share|improve this question


















  • 1





    what is your exact expected outcome for the example string?

    – Jean-François Fabre
    Nov 15 '18 at 16:10











  • no matter how many column details are there i want them to be printed on screen.

    – c0der
    Nov 15 '18 at 16:13











  • Actually i want to print them in the following way :- column name = col1 ,type=decimal, length=10 and precision=2

    – c0der
    Nov 15 '18 at 16:14














0












0








0








CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20))


From this query String i want to get each of the column details (ie, col1 decimal(10,2) and col2 char(20) etc ) as a list in a single group(#anynumber) call.
currently what i'am doing is given below :



columnResult = re.match(r"^CREATE(sw+s*){1,}((w+)s(S+),s*(w+)s(S+)",line)
if columnResult == None:
pass
else:
print("column = ",columnResult.group(2),",",columnResult.group(4))
print("type = ",columnResult.group(3),", ",columnResult.group(5))


Instead of repeating (w+)s(S+) for each column ,how can i match all repeating column details in one shot?
Please help










share|improve this question














CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20))


From this query String i want to get each of the column details (ie, col1 decimal(10,2) and col2 char(20) etc ) as a list in a single group(#anynumber) call.
currently what i'am doing is given below :



columnResult = re.match(r"^CREATE(sw+s*){1,}((w+)s(S+),s*(w+)s(S+)",line)
if columnResult == None:
pass
else:
print("column = ",columnResult.group(2),",",columnResult.group(4))
print("type = ",columnResult.group(3),", ",columnResult.group(5))


Instead of repeating (w+)s(S+) for each column ,how can i match all repeating column details in one shot?
Please help







python regex python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 16:07









c0derc0der

9591714




9591714








  • 1





    what is your exact expected outcome for the example string?

    – Jean-François Fabre
    Nov 15 '18 at 16:10











  • no matter how many column details are there i want them to be printed on screen.

    – c0der
    Nov 15 '18 at 16:13











  • Actually i want to print them in the following way :- column name = col1 ,type=decimal, length=10 and precision=2

    – c0der
    Nov 15 '18 at 16:14














  • 1





    what is your exact expected outcome for the example string?

    – Jean-François Fabre
    Nov 15 '18 at 16:10











  • no matter how many column details are there i want them to be printed on screen.

    – c0der
    Nov 15 '18 at 16:13











  • Actually i want to print them in the following way :- column name = col1 ,type=decimal, length=10 and precision=2

    – c0der
    Nov 15 '18 at 16:14








1




1





what is your exact expected outcome for the example string?

– Jean-François Fabre
Nov 15 '18 at 16:10





what is your exact expected outcome for the example string?

– Jean-François Fabre
Nov 15 '18 at 16:10













no matter how many column details are there i want them to be printed on screen.

– c0der
Nov 15 '18 at 16:13





no matter how many column details are there i want them to be printed on screen.

– c0der
Nov 15 '18 at 16:13













Actually i want to print them in the following way :- column name = col1 ,type=decimal, length=10 and precision=2

– c0der
Nov 15 '18 at 16:14





Actually i want to print them in the following way :- column name = col1 ,type=decimal, length=10 and precision=2

– c0der
Nov 15 '18 at 16:14












1 Answer
1






active

oldest

votes


















2














Try Regex: (?:(?<=, )|(?<=(|,))(w+) (w+)(?:((d+)(?:,(d+))?))?



Demo






share|improve this answer


























  • Hi its working perfectly for CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20)) statements..what if the statement is like CREATE TABLE HIVE_TABLE(foo INT, bar STRING,cat INT) . ie ,without the 'if not exist' .can you please modify the regex so that it will work for both kind of statements?

    – c0der
    Nov 16 '18 at 8:10











  • @c0der, I've updated the regex (and demo) to handle the new requirement.

    – Matt.G
    Nov 16 '18 at 14:24











  • for python it is showing "(?<= A lookbehind assertion has to be fixed width ) A lookbehind assertion has to be fixed width" error

    – c0der
    Nov 17 '18 at 5:29











  • @c0der, updated answer for python

    – Matt.G
    Nov 18 '18 at 12:02












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323454%2fpython-regular-expression-matching-repeating-pattern-in-one-shot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Try Regex: (?:(?<=, )|(?<=(|,))(w+) (w+)(?:((d+)(?:,(d+))?))?



Demo






share|improve this answer


























  • Hi its working perfectly for CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20)) statements..what if the statement is like CREATE TABLE HIVE_TABLE(foo INT, bar STRING,cat INT) . ie ,without the 'if not exist' .can you please modify the regex so that it will work for both kind of statements?

    – c0der
    Nov 16 '18 at 8:10











  • @c0der, I've updated the regex (and demo) to handle the new requirement.

    – Matt.G
    Nov 16 '18 at 14:24











  • for python it is showing "(?<= A lookbehind assertion has to be fixed width ) A lookbehind assertion has to be fixed width" error

    – c0der
    Nov 17 '18 at 5:29











  • @c0der, updated answer for python

    – Matt.G
    Nov 18 '18 at 12:02
















2














Try Regex: (?:(?<=, )|(?<=(|,))(w+) (w+)(?:((d+)(?:,(d+))?))?



Demo






share|improve this answer


























  • Hi its working perfectly for CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20)) statements..what if the statement is like CREATE TABLE HIVE_TABLE(foo INT, bar STRING,cat INT) . ie ,without the 'if not exist' .can you please modify the regex so that it will work for both kind of statements?

    – c0der
    Nov 16 '18 at 8:10











  • @c0der, I've updated the regex (and demo) to handle the new requirement.

    – Matt.G
    Nov 16 '18 at 14:24











  • for python it is showing "(?<= A lookbehind assertion has to be fixed width ) A lookbehind assertion has to be fixed width" error

    – c0der
    Nov 17 '18 at 5:29











  • @c0der, updated answer for python

    – Matt.G
    Nov 18 '18 at 12:02














2












2








2







Try Regex: (?:(?<=, )|(?<=(|,))(w+) (w+)(?:((d+)(?:,(d+))?))?



Demo






share|improve this answer















Try Regex: (?:(?<=, )|(?<=(|,))(w+) (w+)(?:((d+)(?:,(d+))?))?



Demo







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 '18 at 12:01

























answered Nov 15 '18 at 16:22









Matt.GMatt.G

2,7061416




2,7061416













  • Hi its working perfectly for CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20)) statements..what if the statement is like CREATE TABLE HIVE_TABLE(foo INT, bar STRING,cat INT) . ie ,without the 'if not exist' .can you please modify the regex so that it will work for both kind of statements?

    – c0der
    Nov 16 '18 at 8:10











  • @c0der, I've updated the regex (and demo) to handle the new requirement.

    – Matt.G
    Nov 16 '18 at 14:24











  • for python it is showing "(?<= A lookbehind assertion has to be fixed width ) A lookbehind assertion has to be fixed width" error

    – c0der
    Nov 17 '18 at 5:29











  • @c0der, updated answer for python

    – Matt.G
    Nov 18 '18 at 12:02



















  • Hi its working perfectly for CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20)) statements..what if the statement is like CREATE TABLE HIVE_TABLE(foo INT, bar STRING,cat INT) . ie ,without the 'if not exist' .can you please modify the regex so that it will work for both kind of statements?

    – c0der
    Nov 16 '18 at 8:10











  • @c0der, I've updated the regex (and demo) to handle the new requirement.

    – Matt.G
    Nov 16 '18 at 14:24











  • for python it is showing "(?<= A lookbehind assertion has to be fixed width ) A lookbehind assertion has to be fixed width" error

    – c0der
    Nov 17 '18 at 5:29











  • @c0der, updated answer for python

    – Matt.G
    Nov 18 '18 at 12:02

















Hi its working perfectly for CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20)) statements..what if the statement is like CREATE TABLE HIVE_TABLE(foo INT, bar STRING,cat INT) . ie ,without the 'if not exist' .can you please modify the regex so that it will work for both kind of statements?

– c0der
Nov 16 '18 at 8:10





Hi its working perfectly for CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20)) statements..what if the statement is like CREATE TABLE HIVE_TABLE(foo INT, bar STRING,cat INT) . ie ,without the 'if not exist' .can you please modify the regex so that it will work for both kind of statements?

– c0der
Nov 16 '18 at 8:10













@c0der, I've updated the regex (and demo) to handle the new requirement.

– Matt.G
Nov 16 '18 at 14:24





@c0der, I've updated the regex (and demo) to handle the new requirement.

– Matt.G
Nov 16 '18 at 14:24













for python it is showing "(?<= A lookbehind assertion has to be fixed width ) A lookbehind assertion has to be fixed width" error

– c0der
Nov 17 '18 at 5:29





for python it is showing "(?<= A lookbehind assertion has to be fixed width ) A lookbehind assertion has to be fixed width" error

– c0der
Nov 17 '18 at 5:29













@c0der, updated answer for python

– Matt.G
Nov 18 '18 at 12:02





@c0der, updated answer for python

– Matt.G
Nov 18 '18 at 12:02




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323454%2fpython-regular-expression-matching-repeating-pattern-in-one-shot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma