RegEx for parsing ID3 tags

I am creating an id3tag mp3 editor and the regex does not match. Can anyone help me please? my code:

arquivo = "[coletanea] album [CD #] [faixa] [artista] musica.mp3"

r = New Regex("^\[(?<1>[^\]]+?)\]\s*(?<2>[\w\s]+)\s*\[CD\s*(?<3>\d+)\]\s*\[(?<4>\d+)\]\s*\[(?<5>[^\]]+)\]\s*(?<6>.+)", RegexOptions.Compiled)
m = r.Match(Mid(arquivo, 1, Len(arquivo) - 4))
        If m.Success Then
            mAuthor = Trim(m.Groups(5).ToString)
            mWM_AlbumTitle = Trim(m.Groups(2).ToString)
            mWM_TrackNumber = Trim(m.Groups(4).ToString)
            mTitle = Trim(m.Groups(6).ToString)
            mWM_PartOfSet = Trim(m.Groups(3).ToString)
            mMW_AlbumArtist = Trim(m.Groups(1).ToString)
        End If

      

0


source to share


2 answers


This is because your string will never match the regex you are using.

The regular expression expects a number instead of # and a number instead of "faixa"

Try this for example:



"[coletanea] album [CD 20] [89] [artista] musica.mp3"

      

If you want to allow any character instead of prime numbers, replace \ d for. in groups 3 and 4

+4


source


Well your dosen't line matches regex (since "#" and "faixa" are not numbers) try this line, I assume this is the string you get from the system:

"[coletanea] album [CD 1] [1] [artista] musica.mp3"

      



It's also a bit weird to name capture groups for numbers like this, because by default group names will start at 0.

+1


source







All Articles