Python 3.7: Using regex on a string with whitespace in the beginning











up vote
0
down vote

favorite












I have lines in a text file that I need to parse with regular expressions and assign numbers to variables. Here is an example of what the lines looks like



      1   35 A K              0   0  182


So there is whitespace in the beginning of the string, and whenever I use this website (https://pythex.org/) it does not seem to match anything when I use the regular expression



^s+d+s+d+swsws+ds+ds+d


How can I use regular expressions to assign 182 to a variable?










share|improve this question




















  • 4




    You're missing a backslash on the first s+. Try changing it to ^s+d+s+d+swsws+ds+ds+d
    – Davіd
    Nov 8 at 2:35















up vote
0
down vote

favorite












I have lines in a text file that I need to parse with regular expressions and assign numbers to variables. Here is an example of what the lines looks like



      1   35 A K              0   0  182


So there is whitespace in the beginning of the string, and whenever I use this website (https://pythex.org/) it does not seem to match anything when I use the regular expression



^s+d+s+d+swsws+ds+ds+d


How can I use regular expressions to assign 182 to a variable?










share|improve this question




















  • 4




    You're missing a backslash on the first s+. Try changing it to ^s+d+s+d+swsws+ds+ds+d
    – Davіd
    Nov 8 at 2:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have lines in a text file that I need to parse with regular expressions and assign numbers to variables. Here is an example of what the lines looks like



      1   35 A K              0   0  182


So there is whitespace in the beginning of the string, and whenever I use this website (https://pythex.org/) it does not seem to match anything when I use the regular expression



^s+d+s+d+swsws+ds+ds+d


How can I use regular expressions to assign 182 to a variable?










share|improve this question















I have lines in a text file that I need to parse with regular expressions and assign numbers to variables. Here is an example of what the lines looks like



      1   35 A K              0   0  182


So there is whitespace in the beginning of the string, and whenever I use this website (https://pythex.org/) it does not seem to match anything when I use the regular expression



^s+d+s+d+swsws+ds+ds+d


How can I use regular expressions to assign 182 to a variable?







regex python-3.7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 2:45









Barmar

413k34239340




413k34239340










asked Nov 8 at 2:28









flannel_bioinformatician

385




385








  • 4




    You're missing a backslash on the first s+. Try changing it to ^s+d+s+d+swsws+ds+ds+d
    – Davіd
    Nov 8 at 2:35














  • 4




    You're missing a backslash on the first s+. Try changing it to ^s+d+s+d+swsws+ds+ds+d
    – Davіd
    Nov 8 at 2:35








4




4




You're missing a backslash on the first s+. Try changing it to ^s+d+s+d+swsws+ds+ds+d
– Davіd
Nov 8 at 2:35




You're missing a backslash on the first s+. Try changing it to ^s+d+s+d+swsws+ds+ds+d
– Davіd
Nov 8 at 2:35












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You can use the following regex.



^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)


You can put the last number in a group which you can capture in your code.



Following is a test python script.



import re

regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"

test_str = " 1 35 A K 0 0 182"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches):
matchNum = matchNum + 1

print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1

print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))


Output:



Match 1 was found at 0-40:       1   35 A K              0   0  182
Group 1 found at 37-40: 182





share|improve this answer





















    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',
    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%2f53200705%2fpython-3-7-using-regex-on-a-string-with-whitespace-in-the-beginning%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








    up vote
    0
    down vote













    You can use the following regex.



    ^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)


    You can put the last number in a group which you can capture in your code.



    Following is a test python script.



    import re

    regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"

    test_str = " 1 35 A K 0 0 182"

    matches = re.finditer(regex, test_str, re.MULTILINE)

    for matchNum, match in enumerate(matches):
    matchNum = matchNum + 1

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
    groupNum = groupNum + 1

    print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))


    Output:



    Match 1 was found at 0-40:       1   35 A K              0   0  182
    Group 1 found at 37-40: 182





    share|improve this answer

























      up vote
      0
      down vote













      You can use the following regex.



      ^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)


      You can put the last number in a group which you can capture in your code.



      Following is a test python script.



      import re

      regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"

      test_str = " 1 35 A K 0 0 182"

      matches = re.finditer(regex, test_str, re.MULTILINE)

      for matchNum, match in enumerate(matches):
      matchNum = matchNum + 1

      print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

      for groupNum in range(0, len(match.groups())):
      groupNum = groupNum + 1

      print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))


      Output:



      Match 1 was found at 0-40:       1   35 A K              0   0  182
      Group 1 found at 37-40: 182





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can use the following regex.



        ^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)


        You can put the last number in a group which you can capture in your code.



        Following is a test python script.



        import re

        regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"

        test_str = " 1 35 A K 0 0 182"

        matches = re.finditer(regex, test_str, re.MULTILINE)

        for matchNum, match in enumerate(matches):
        matchNum = matchNum + 1

        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

        for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))


        Output:



        Match 1 was found at 0-40:       1   35 A K              0   0  182
        Group 1 found at 37-40: 182





        share|improve this answer












        You can use the following regex.



        ^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)


        You can put the last number in a group which you can capture in your code.



        Following is a test python script.



        import re

        regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"

        test_str = " 1 35 A K 0 0 182"

        matches = re.finditer(regex, test_str, re.MULTILINE)

        for matchNum, match in enumerate(matches):
        matchNum = matchNum + 1

        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

        for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))


        Output:



        Match 1 was found at 0-40:       1   35 A K              0   0  182
        Group 1 found at 37-40: 182






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 3:16









        Bilesh Ganguly

        1,72811533




        1,72811533






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53200705%2fpython-3-7-using-regex-on-a-string-with-whitespace-in-the-beginning%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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings