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

  • Home
  • SEARCH
  • 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 8144065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:16:19+00:00 2026-06-06T13:16:19+00:00

I am trying to write a function to parse the string representation of a

  • 0

I am trying to write a function to parse the string representation of a musical chord.

Example: C major chord -> Cmaj (this is what I want to parse)

Just to make it clear, a chord is made of three different parts:

  • the note (C, D, E, F, G, A)
  • the accidentals for that note (#, ##, b, bb)
  • the chord name

For those, music savvy, I am not considering slash chords (on purpose).

The below function is almost working. However it still doesn’t work for the following case:

  • “C#maj” # matches and should
  • “C#maj7” # matches and should
  • “C#maj2” # mathches and shouldn’t

I suppose that if I could make the chords regex part forced to be at the end of the regex, did the trick. I have tried using the $ both before and after this String but it didn’t work.

Any idea? Thanks.

public static void regex(String chord) {                
    String notes = "^[CDEFGAB]";
    String accidentals = "[#|##|b|bb]";
    String chords = "[maj7|maj|min7|min|sus2]";
    String regex = notes + accidentals + chords; 
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(chord);
    System.out.println("regex is " + regex);
    if (matcher.find()) {
        int i = matcher.start();
        int j = matcher.end();
        System.out.println("i:" + i + " j:" + j);           
    }
    else {
        System.out.println("no match!");
    }
}
  • 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-06T13:16:20+00:00Added an answer on June 6, 2026 at 1:16 pm

    Change [ and ] to ( and ) in the following lines:

    String accidentals = "(#|##|b|bb)";
    String chords = "(maj7|maj|min7|min|sus2)";
    

    Otherwise you’re just making character classes, so [maj7|maj|min7|min|sus2] simply matches on the letter m.

    I’m guessing you also want to add an ending anchor $? I see you had problems with that before, but that’s probably because of the aforementioned issue.


    Also, might you want (#|##|b|bb) to be optional (i.e., with ?: (#|##|b|bb)?)?

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

Sidebar

Related Questions

I'm trying to write function and i dont know why i cannot make it
I'm trying to write a function to return the word string of any number
I'm trying to write an sql function in Postgresql that will parse a file
I wrote this code in vb: expireDate = (DateTime.Parse(decal.DecalExpireDate).ToShortDateString).ToString Now I'm trying to write
I am trying to write a javascript function to parse a bit of the
I'm having some trouble with this code. I am trying to write a function
I'm trying to write a python function to parse the width and height from
I had to write the following function to fail gracefully when trying to parse
I'm trying to parse a delimited string that has some empty parameters. Example: |One|two|three||octopus|garbagecan||cartwheel||||
I'm trying to write up an example of testing query string parsing when I

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.