0

I'm having some trouble using Regex to match an exact string (and only that string, must not contain prefix or suffix) out of a sample with only slight variations.

I've looked over every "duplicate" this has been compared to, and none of the solutions seem to apply to the problem I'm trying to accomplish. If it is indeed a duplicate, I'd love to see how!

Phrase to Match: Metallica - Master Of Puppets

Sample Text:

Metallica - Master Of Puppets (instrumental)

Metallica - Master Of Puppets

- Metallica - Master Of Puppets

I've tried a few different approaches with this.

  • There's "the starting point": ^(Metallica - Master Of Puppets)$
  • The "slightly more involved": ^((?!Lamb of God - Laid to Rest).)*$
  • The "I'm getting desperate": /(?<=\||\A|\n)(Metallica - .Master.of.Puppets)(?=\||Z|\n)/g
  • and the "im out of ideas, so why the hell not": (?=^\s*Metallica - Master Of Puppets).{29}

None of which will match the correct (the second option, in bold) string. I've dedicated the better part of my free-time from last night on this one little string rather than coding out a new app I've been working on (I really do hate to give up), and am, at this point, out of ideas, examples and patience. Nonetheless, I really would like to get to the bottom of what this seemingly simple Regex need will take to accomplish, both for the app and for my mental well being (I hate Regex, but love a good challenge).

Note: I DO need this to be done in Regex (not grep, not java etc). Sorry to throw such a seemingly menial question up, but my 15 or so months in the programming world only gets me so far. Looking forward to a solution, thanks!5

sbm128
  • 11
  • 1
  • 1
  • 2
  • 1
    Word boundaries? `\bMetallica - Master Of Puppets\b`? – Wiktor Stribiżew Jul 23 '16 at 11:48
  • The second and the third text string look exactly the same (let alone the formatting). Based on what criteria would you like to pick up the second string only? – Dmitry Egorov Jul 23 '16 at 11:48
  • I don't think this question is a duplicate but that's because I think it makes no sense (i.e. I'd vote for closing as unclear). – melpomene Jul 23 '16 at 11:52
  • Sorry, formatting issues cleared up - the difference between the two is a "- " before the 3rd. This is just an example of variation to expect. What doesn't make sense exactly melpomene? or did i cover it with the formatting clean-up? The criteria for the 2nd option being the "correct" one is that there is no prefix or suffix to the example. "Band Name - Song Name" is the format. Any variation outside of that could illicit the incorrect results. – sbm128 Jul 23 '16 at 12:13
  • Then your first attempt should be fine: https://regex101.com/r/dA0mW5/1 – Dmitry Egorov Jul 23 '16 at 12:16
  • 2
    The part that makes no sense is "*I DO need this to be done in Regex (not grep, not java etc)*". What language or tool are you actually using? – melpomene Jul 23 '16 at 20:15
  • @melpomene yup, seconded. Regex is not the tool for this. Why couldn't the equality operator work for this? – Nissa Jul 23 '16 at 21:12

1 Answers1

4

I believe your first approach was correct (in agreement with @Dmitry Egorov), although you are probably missing the multi-line flag. This sets it so that ^ and $ are set at the beginning and end of each line of your string or file.

In PHP/Js you'll want to use

/^Metallica - Master Of Puppets$/gm

the g flag is 'global' and finds all instances, the mflag is the aforementioned multi-line flag.

Other languages will have similar flags or options for multi-line support.

Skogsv
  • 494
  • 3
  • 14