Note that there are some explanatory texts on larger screens.

plurals
  1. POI need help figuring out why my regular expression doesn't seem deterministic
    text
    copied!<p>I use a regular expression to extract chords from input text files. While it works most of the time it fails on a certain file.</p> <p>This is my regexp code:</p> <pre><code>def getChordMatches(line): import re notes = "[ABCDEFG]"; accidentals = "(?:#|##|b|bb)?"; chords = "(?:maj|min|m|sus|aug|dim)?" additions = "[0-9]?" chordFormPattern = notes + accidentals + chords + additions fullPattern = chordFormPattern + "(?:/%s)?\s" % (notes + accidentals) matches = [removeWhitespaces(x) for x in re.findall(fullPattern, line)] positions = [x.start() for x in re.finditer(fullPattern, line)] return matches, positions </code></pre> <p>This is the result when it works:</p> <pre><code> line: Em C C/B matches: [u'Em', u'C', u'C/B'] position: [5, 20, 23] </code></pre> <p>This line is from a file that doesn't produce the correct result:</p> <pre><code> line:   Am           Am/G       D7/F#                 Fmaj7 matches: [u'Fmaj7'] position: [48] </code></pre> <p>Where should I start digging? Encoding, special characters, tabs, ... ?</p> <p><em>edit</em></p> <p>This is where above output is from:</p> <pre><code>line = unicode(l, encoding='utf-8') matches, positions = getChordMatches(line) print ' line:', line print ' matches:', matches print 'position:', positions </code></pre> <p><em>edit</em></p> <p>The full regex pattern is: </p> <pre><code>[ABCDEFG](?:#|##|b|bb)?(?:maj|min|m|sus|aug|dim)?[0-9]?(?:/[ABCDEFG](?:#|##|b|bb)?)?\s </code></pre> <p><em>edit</em></p> <p>A hexdump of the failing line (I think):</p> <pre><code>hexdump -s 45 -n 99 input.txt 000002d 20 41 6d 20 20 20 20 20 20 20 20 20 20 41 6d 2f 000003d 47 20 c2 a0 20 20 20 20 20 20 44 37 2f 46 23 20 000004d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 000005d 46 6d 61 6a 37 0a 49 20 6c 6f 6f 6b 20 61 74 20 000006d 79 6f 75 20 61 6c 6c 20 73 65 65 20 74 68 65 20 000007d 6c 6f 76 65 20 74 68 65 72 65 20 74 68 61 74 27 000008d 73 20 73 0000090 </code></pre> <p><em>edit</em></p> <p>As mentioned in the accepted answer it was caused by a non breaking space. Using <code>line = unicode(l, encoding='utf-8').replace(u"\u00A0", " ")</code> solves the problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload