Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:51:38+00:00 2026-06-16T22:51:38+00:00

I use a regular expression to extract chords from input text files. While it

  • 0

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.

This is my regexp 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

This is the result when it works:

    line:      Em             C  C/B
 matches: [u'Em', u'C', u'C/B']
position: [5, 20, 23]

This line is from a file that doesn’t produce the correct result:

    line:   Am           Am/G       D7/F#                 Fmaj7
 matches: [u'Fmaj7']
position: [48]

Where should I start digging? Encoding, special characters, tabs, … ?

edit

This is where above output is from:

line = unicode(l, encoding='utf-8') 
matches, positions = getChordMatches(line) 
print '    line:', line 
print ' matches:', matches 
print 'position:', positions

edit

The full regex pattern is:

[ABCDEFG](?:#|##|b|bb)?(?:maj|min|m|sus|aug|dim)?[0-9]?(?:/[ABCDEFG](?:#|##|b|bb)?)?\s

edit

A hexdump of the failing line (I think):

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

edit

As mentioned in the accepted answer it was caused by a non breaking space. Using line = unicode(l, encoding='utf-8').replace(u"\u00A0", " ") solves the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:51:40+00:00Added an answer on June 16, 2026 at 10:51 pm

    I suspect the problem has to do with the following two bytes:

    000003d 47 20 c2 a0 20 20 …

    This seems to be a UTF-8 encoded non-breaking space (U+00A0). It wouldn’t surprise me if this is what’s tripping up your regex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use regular expression to extract date from a string as below
I would like to use a regular expression to extract bind variable parameters from
I would like to use regular expression to extract only @patrick @michelle from the
I have that text : <a href=/extend/themes/bizway>BizWay</a> And i want to use regular expression
I want to use this regular expression in Python: <(?:[^]*[']*|'[^']*'[']*|[^'>])+> (from RegEx match open
I have created the following Regular Expression in C# to extract tokens from a
My application has a feature that parses text using a regular expression to extract
How to use regular expression in prev()? <div class=aaa>aaa</div> <div style=display: none class=bbb>bbb</div> <div
I want to use regular expression to find strings in a file that have
How to use regular expression in iOS sdk? I need to validate UITextField using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.