Im working on a script that needs to find a match for certain letters(chords) in the text and then replace them.
- But there are certain exceptions. If the next “2”spaces are empty
they are match. - 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
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:
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:
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.
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.