Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by StellarDiscoverer331

How do I build a C# Regex to accurately validate music note formats?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm developing a C# project to handle music notes and need a regex that verifies the note input follows a specific format.

The requirements are:

  • Valid note letters: A, C, D, F, and G (which can optionally have a '#' appended) plus digits.
  • For notes B and E, the '#' is not allowed; they must be followed directly by one or more digits.

Valid examples:

C1
C#1
B1

Invalid examples:

B#1
CCCCC1
C####1
1C
I AM A FISH
C
H (or any character not in the defined list)
H#1
H1

Currently, I'm using the following code, but it incorrectly marks entries like "CCCCCCC1" and "B#1" as valid:

CSHARP
Regex h = new Regex(@"(((A|C|D|F|G)?(#?))|(B|E))?\d\d?", RegexOptions.IgnoreCase); Match X = h.Match(Console.ReadLine()); if (X.Success) { Console.WriteLine("VALID"); } else { Console.WriteLine("INVALID"); } Console.ReadLine();

I'd appreciate any guidance on constructing a regex that properly accepts valid inputs (like "C1", "C#1", "B1") and rejects invalid ones.

2

Comments 0

Answers 3

Views 84

3 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Below is one correct solution. When designing your regex, it’s best to anchor it (using ^ and $) so that the entire input is matched rather than a substring. In this case you want either:

• A note letter in the set A, C, D, F, or G (case insensitive) with an optional “#”

• Or a note letter in the set B or E (without a “#”)

After that, you require one or more digits.

A suitable regex is:

CSHARP
Regex h = new Regex(@"^(?:(?:[ACDFG]#?)|[BE])\d+$", RegexOptions.IgnoreCase);

Explanation

  • The caret (^) and dollar ($) anchors ensure that the entire string is matched.
  • The non-capturing group (?:(?:[ACDFG]#?)|[BE]) expresses that:
    • Either one letter from A, C, D, F, or G optionally followed by a literal # (using #?), or
    • One letter from B or E (with no sharp allowed)
  • Finally, \d+ ensures that one or more digits follow.
  • The RegexOptions.IgnoreCase flag handles case insensitivity.

This regex correctly handles your valid cases (e.g., “C1”, “C#1”, “B1”) and rejects invalid inputs like “B#1”, “CCCCCCC1”, etc.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by MartianSentinel125 1 month ago

0

Try This

CSHARP
Regex h = new Regex(@"^([A-D|F-G]#?\d{1,2}|[B|E]\d{1,2})$",RegexOptions.IgnoreCase);

No comments yet.

Answer by CometWatcher629 1 month ago

0

I think this pattern can be simplified to just ^([ACDFG]#?|[BE])\d$

It matches all your valid inputs and rejects all your invalid. I have removed the capture groups for clarity, if you needed those just wrap the parentheses in the correct place ()

Your original code had a potential for 2 integers - you can use \d{1,2} to represent that if need be.

Demo: https://regexr.com/8b6vm

No comments yet.

Discussion

No comments yet.