Python re.finditer error at special characters












1















I have code that loops through a list of strings, then loops through each occurrence of that string inside another string. This seems to work until it reaches a string that begins with a question mark (? I).



This is the code.



dtID = 0
for datum in sorted(datumList, key=operator.attrgetter('Sum'), reverse = True):
datum.ID = dtID
for foundDatum in re.finditer(datum.Name, text):
datumLocList.append(DatumLoc(dtID,foundDatum.start()))
dtID += 1


How can I solve this?



Traceback (most recent call last):
File "C:UserstristDocumentsPythonThe CompressorThe Compressor.py", line 97, in <module>
compress()
File "C:UserstristDocumentsPythonThe CompressorThe Compressor.py", line 73, in compress
for foundDatum in re.finditer(datum.Name, text):
File "C:Program FilesPython37libre.py", line 230, in finditer
return _compile(pattern, flags).finditer(string)
File "C:Program FilesPython37libre.py", line 286, in _compile
p = sre_compile.compile(pattern, flags)
File "C:Program FilesPython37libsre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "C:Program FilesPython37libsre_parse.py", line 930, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File "C:Program FilesPython37libsre_parse.py", line 426, in _parse_sub
not nested and not items))
File "C:Program FilesPython37libsre_parse.py", line 651, in _parse
source.tell() - here + len(this))
re.error: nothing to repeat at position 0









share|improve this question



























    1















    I have code that loops through a list of strings, then loops through each occurrence of that string inside another string. This seems to work until it reaches a string that begins with a question mark (? I).



    This is the code.



    dtID = 0
    for datum in sorted(datumList, key=operator.attrgetter('Sum'), reverse = True):
    datum.ID = dtID
    for foundDatum in re.finditer(datum.Name, text):
    datumLocList.append(DatumLoc(dtID,foundDatum.start()))
    dtID += 1


    How can I solve this?



    Traceback (most recent call last):
    File "C:UserstristDocumentsPythonThe CompressorThe Compressor.py", line 97, in <module>
    compress()
    File "C:UserstristDocumentsPythonThe CompressorThe Compressor.py", line 73, in compress
    for foundDatum in re.finditer(datum.Name, text):
    File "C:Program FilesPython37libre.py", line 230, in finditer
    return _compile(pattern, flags).finditer(string)
    File "C:Program FilesPython37libre.py", line 286, in _compile
    p = sre_compile.compile(pattern, flags)
    File "C:Program FilesPython37libsre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
    File "C:Program FilesPython37libsre_parse.py", line 930, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
    File "C:Program FilesPython37libsre_parse.py", line 426, in _parse_sub
    not nested and not items))
    File "C:Program FilesPython37libsre_parse.py", line 651, in _parse
    source.tell() - here + len(this))
    re.error: nothing to repeat at position 0









    share|improve this question

























      1












      1








      1








      I have code that loops through a list of strings, then loops through each occurrence of that string inside another string. This seems to work until it reaches a string that begins with a question mark (? I).



      This is the code.



      dtID = 0
      for datum in sorted(datumList, key=operator.attrgetter('Sum'), reverse = True):
      datum.ID = dtID
      for foundDatum in re.finditer(datum.Name, text):
      datumLocList.append(DatumLoc(dtID,foundDatum.start()))
      dtID += 1


      How can I solve this?



      Traceback (most recent call last):
      File "C:UserstristDocumentsPythonThe CompressorThe Compressor.py", line 97, in <module>
      compress()
      File "C:UserstristDocumentsPythonThe CompressorThe Compressor.py", line 73, in compress
      for foundDatum in re.finditer(datum.Name, text):
      File "C:Program FilesPython37libre.py", line 230, in finditer
      return _compile(pattern, flags).finditer(string)
      File "C:Program FilesPython37libre.py", line 286, in _compile
      p = sre_compile.compile(pattern, flags)
      File "C:Program FilesPython37libsre_compile.py", line 764, in compile
      p = sre_parse.parse(p, flags)
      File "C:Program FilesPython37libsre_parse.py", line 930, in parse
      p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
      File "C:Program FilesPython37libsre_parse.py", line 426, in _parse_sub
      not nested and not items))
      File "C:Program FilesPython37libsre_parse.py", line 651, in _parse
      source.tell() - here + len(this))
      re.error: nothing to repeat at position 0









      share|improve this question














      I have code that loops through a list of strings, then loops through each occurrence of that string inside another string. This seems to work until it reaches a string that begins with a question mark (? I).



      This is the code.



      dtID = 0
      for datum in sorted(datumList, key=operator.attrgetter('Sum'), reverse = True):
      datum.ID = dtID
      for foundDatum in re.finditer(datum.Name, text):
      datumLocList.append(DatumLoc(dtID,foundDatum.start()))
      dtID += 1


      How can I solve this?



      Traceback (most recent call last):
      File "C:UserstristDocumentsPythonThe CompressorThe Compressor.py", line 97, in <module>
      compress()
      File "C:UserstristDocumentsPythonThe CompressorThe Compressor.py", line 73, in compress
      for foundDatum in re.finditer(datum.Name, text):
      File "C:Program FilesPython37libre.py", line 230, in finditer
      return _compile(pattern, flags).finditer(string)
      File "C:Program FilesPython37libre.py", line 286, in _compile
      p = sre_compile.compile(pattern, flags)
      File "C:Program FilesPython37libsre_compile.py", line 764, in compile
      p = sre_parse.parse(p, flags)
      File "C:Program FilesPython37libsre_parse.py", line 930, in parse
      p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
      File "C:Program FilesPython37libsre_parse.py", line 426, in _parse_sub
      not nested and not items))
      File "C:Program FilesPython37libsre_parse.py", line 651, in _parse
      source.tell() - here + len(this))
      re.error: nothing to repeat at position 0






      python-3.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 11:42









      Tristan KingTristan King

      659




      659
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Your pattern string containing a question mark is being treated as a regular expression special character. The ? symbol attempts to match 0 or 1 repetitions of the preceding regular expression. Since in your string the ? is the first character, it is attempting to match 0 or 1 repetitions of the preceding regular expression, which is nothing: hence your 'nothing to repeat at position 0' error.



          To avoid this you can use the re.escape() method which will escape all RE special characters in your pattern string.



          for foundDatum in re.finditer(re.escape(datum.Name), text):
          datumLocList.append(DatumLoc(dtID,foundDatum.start()))


          See https://docs.python.org/3/library/re.html#re.escape






          share|improve this answer
























          • Works great, thanks.

            – Tristan King
            Nov 23 '18 at 12:51












          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%2f53446061%2fpython-re-finditer-error-at-special-characters%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









          1














          Your pattern string containing a question mark is being treated as a regular expression special character. The ? symbol attempts to match 0 or 1 repetitions of the preceding regular expression. Since in your string the ? is the first character, it is attempting to match 0 or 1 repetitions of the preceding regular expression, which is nothing: hence your 'nothing to repeat at position 0' error.



          To avoid this you can use the re.escape() method which will escape all RE special characters in your pattern string.



          for foundDatum in re.finditer(re.escape(datum.Name), text):
          datumLocList.append(DatumLoc(dtID,foundDatum.start()))


          See https://docs.python.org/3/library/re.html#re.escape






          share|improve this answer
























          • Works great, thanks.

            – Tristan King
            Nov 23 '18 at 12:51
















          1














          Your pattern string containing a question mark is being treated as a regular expression special character. The ? symbol attempts to match 0 or 1 repetitions of the preceding regular expression. Since in your string the ? is the first character, it is attempting to match 0 or 1 repetitions of the preceding regular expression, which is nothing: hence your 'nothing to repeat at position 0' error.



          To avoid this you can use the re.escape() method which will escape all RE special characters in your pattern string.



          for foundDatum in re.finditer(re.escape(datum.Name), text):
          datumLocList.append(DatumLoc(dtID,foundDatum.start()))


          See https://docs.python.org/3/library/re.html#re.escape






          share|improve this answer
























          • Works great, thanks.

            – Tristan King
            Nov 23 '18 at 12:51














          1












          1








          1







          Your pattern string containing a question mark is being treated as a regular expression special character. The ? symbol attempts to match 0 or 1 repetitions of the preceding regular expression. Since in your string the ? is the first character, it is attempting to match 0 or 1 repetitions of the preceding regular expression, which is nothing: hence your 'nothing to repeat at position 0' error.



          To avoid this you can use the re.escape() method which will escape all RE special characters in your pattern string.



          for foundDatum in re.finditer(re.escape(datum.Name), text):
          datumLocList.append(DatumLoc(dtID,foundDatum.start()))


          See https://docs.python.org/3/library/re.html#re.escape






          share|improve this answer













          Your pattern string containing a question mark is being treated as a regular expression special character. The ? symbol attempts to match 0 or 1 repetitions of the preceding regular expression. Since in your string the ? is the first character, it is attempting to match 0 or 1 repetitions of the preceding regular expression, which is nothing: hence your 'nothing to repeat at position 0' error.



          To avoid this you can use the re.escape() method which will escape all RE special characters in your pattern string.



          for foundDatum in re.finditer(re.escape(datum.Name), text):
          datumLocList.append(DatumLoc(dtID,foundDatum.start()))


          See https://docs.python.org/3/library/re.html#re.escape







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 12:04









          Robbie DunnRobbie Dunn

          864




          864













          • Works great, thanks.

            – Tristan King
            Nov 23 '18 at 12:51



















          • Works great, thanks.

            – Tristan King
            Nov 23 '18 at 12:51

















          Works great, thanks.

          – Tristan King
          Nov 23 '18 at 12:51





          Works great, thanks.

          – Tristan King
          Nov 23 '18 at 12:51




















          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%2f53446061%2fpython-re-finditer-error-at-special-characters%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini