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 8128993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:07:20+00:00 2026-06-06T08:07:20+00:00

Im working on a script that needs to find a match for certain letters(chords)

  • 0

Im working on a script that needs to find a match for certain letters(chords) in the text and then replace them.

  1. But there are certain exceptions. If the next “2”spaces are empty
    they are match.
  2. If there is only one empty space, but the second space also
    contains a letter that is match with the above rules aswell.

EX (Match these A,Am,B,C#) : And the text is :

Am B
A plane came down C# B

In this example the (Am, the B, C# and B) should get match but not the “A” plane.

Im not really good with string functions and regex any help would be greatly appreciated

Im basically trying to write a transposer, but my major concern is that its going to select the wrong text thanks

  • 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-06T08:07:23+00:00Added an answer on June 6, 2026 at 8:07 am

    Assuming you have no control over the input (e.g. you’re scraping these from tablature websites), and for some reason these chords are interleaved between words instead of resting between lines, then here’s a start:

    <?php
    
        $mods = '(?:maj|m|min|sus|add9|aug|dim|dom|...)';
        $regex = "/\b([ABCDEFG][#b]?$mods?)\s+/"
    
    ?>
    

    I’m afraid, however, I don’t understand your “next 2 spaces” constraint. If you’re trying to distinguish between the chord, “A”, and the word, “A”, though, I advise against your method. Instead, consider the following alternate rule, though far from perfect. “A” is the only chord that is commonly a word by itself, so if your lyrics are well-capitalized, a hint that “A” is a word and not a chord is that the next word is an uncapitalized word:

        Am B A plane came down C# --> Am B C#
    
        Am B A Plane came down C# --> Am B A C#
    

    If you think about it, aside from context clues, this is how humans can tell, too. To make a regex out of this strategy, one would consider the lone “A” a special case, as follows.

    <?php
    
        $mods = '(?:maj|m|min|sus|add9|aug|dim|dom|...)';
        $regex_1 = '((?:A(?!\s)|[BCDEFG])[#b]?$mods?)';
        $regex_2 = '(A(?=\s+[a-z]))';
        $regex = "/\b(?:$regex_1|$regex_2)\s+/";
    
    ?>
    

    See a running demo here: http://rubular.com/r/tRjozL7KCx.

    This is far from perfect but something you can start with and improve.

    UPDATE: An explanation, to help learn.

    \b                      A word-boundary, so "A plane came down and CRASHED
                            into the sea" will not match "D" as a note.
    (?:                     A non-capture group.  (Ignore this for now.)
        (                   A capture group.  (To encapsulate $regex_1.)
            (?:             A non-capture group.  (Ignore this for now.)
                A(?!\s)     An "A" not followed by whitespace, i.e. a "lone A".
                |           An "OR" operator.
                [BCDEFG]    Any one of these characters, B C D E F G.
            )
            [#b]            Any one of the characters, # b.
            ?               Says the previous entity (# or b) is optional.
            $mods           Any one of the modifiers, e.g. maj m min ...
            ?               Says the previous entity (maj m min ...) is optional.
        )
    
        |                   An "OR" operator.
    
        (                   A capture group.  (To encapsulate $regex_2.)
            A(?=\s+[a-z])   An "A" that is followed by at least one whitespace
                            character, then a lower-case letter.
        )
    )
    \s+                     A bunch of whitespace.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Python script that needs to create about 50 distinct temporary
I am working on a PHP script that needs to read log files in
I'm working on a script in PHP that needs to get some info from
So here's something I find puzzling. I'm working on a Javascript that needs to
I'm working on a PHP script that needs to communicate with a service REST
I'm working on a script currently that needs to pull information down from a
I'm working with a script that is calling the same class twice and throwing
I'm working on a script that will check if a user is an Administrator
I am working on a script that will scroll through list elements by clicking
I am working on php script that may involve a long running task. The

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.