Regex Error - Nothing to Repeat

regex error - nothing to repeat

It seems to be a python bug (that works perfectly in vim).
The source of the problem is the (\s*...)+ bit. Basically , you can't do (\s*)+ which make sense , because you are trying to repeat something which can be null.

>>> re.compile(r"(\s*)+")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 180, in compile
return _compile(pattern, flags)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 233, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat

However (\s*\1) should not be null, but we know it only because we know what's in \1. Apparently python doesn't ... that's weird.

Regex error: nothing to repeat at position 0

In regex + means that the preceding match group needs to be repeated one or more times. You placed + at the beginning of the match pattern so there nothing that can be repeated. It seems that you want to match the character +. If so, then + needs to be escaped, and your match pattern should be '\+\+cnt'

Javascript + Regex = Nothing to repeat error?

You need to double the backslashes used to escape the regular expression special characters. However, as @Bohemian points out, most of those backslashes aren't needed. Unfortunately, his answer suffers from the same problem as yours. What you actually want is:

The backslash is being interpreted by the code that reads the string, rather than passed to the regular expression parser. You want:

"[\\[\\]?*+|{}\\\\()@.\n\r]"

Note the quadrupled backslash. That is definitely needed. The string passed to the regular expression compiler is then identical to @Bohemian's string, and works correctly.

Nothing to repeat from Python regex

You do not need the * in the pattern, it causes the issue because you are trying to quantify the beginning of the pattern, but there is nothing, an empty string, to quantify.

The same "Nothing to repeat" error occurs when you

  • Place any quantifier (+, ?, *, {2}, {4,5}, etc.) at the start of the pattern (e.g. re.compile(r'?'))
  • Add any quantifier right after ^ / \A start of string anchor (e.g. re.compile(r'^*'))
  • Add any quantifier right after $ / \Z end of string anchor (e.g. re.compile(r'$*'))
  • Add any quantifier after a word boundary (e.g.re.compile(r'\b*\d{5}'))

Note, however, that in Python re, you may quantify any lookaround, e.g. (?<!\d)*abc and (?<=\d)?abc will yield the same matches since the lookarounds are optional.

Use

([a-zA-Z]+)\.csv

Or to match the whole string:

.*([a-zA-Z]+)\.csv

See demo

The reason is that * is unescaped and is thus treated as a quantifier. It is applied to the preceding subpattern in the regex. Here, it is used in the beginning of a pattern, and thus cannot quantify nothing. Thus, nothing to repeat is thrown.

If it "works" in VIM, it is just because VIM regex engine ignores this subpattern (same as Java does with unescaped [ and ] inside a character class like [([)]]).

How to fix nothing to repeat regex error?

This has little to do with the question you linked. You're not running into a bug. Your regex simply has a special character (a *) that you haven't escaped.

Simply escape the string before compiling it into a regex:

re.compile(re.escape(u'\U0000002A \U000020E3'))

Now, I'm a little unsure as to why you're representing * as \U0000002A — perhaps you could clarify what your intent is here?

Javascript regex error /?/: nothing to repeat It worked fine earlier

You need to escape some characters when using them as literals in a regular expression. Among those are most of the characters you have in your array.

Given your function replaces the obstruction characters with their ASCII code (and some wrapping __i!__), I would suggest to make your function a bit more concise, by performing the replacement with one regular expression, and a callback passed to .replace():

function cleanTitle(title){    return title.replace(/[\\/:*?"<>|]/g, function (ch) {        return "__i!__"+ch.charCodeAt(0)+"__!i__";    });}  
var someTitle = "wh*r* is |his?";var result = cleanTitle(someTitle);
console.log(result);


Related Topics



Leave a reply



Submit