1

I'm attempting to get the values

Title: some, song title
Length: 5:35
Rating: 0
Genre: Other
Size: 7.6 MB

from a string, however I need it so they can be placed anywhere in the string for example..

Title: some, song title, Length: 5:35, Rating: 0, Genre: Other, Size: 7.6 MB
Size: 7.6 MB, Title: some, song title, Length: 5:35, Genre: Other, Rating: 0

Would both return the values above

Thanks

1
  • Are they separated by newline or how do you know when a song ends? Commented Jun 13, 2011 at 15:27

5 Answers 5

3

Well, you could do something like this:

/Title: (.*?)(?=,\s\w+:|$)/               Song Title
/Length: (\d+:\d+)/                       Length
/Rating: (\d+)/                           Rating
/Genre: (.*?)(?=,\s\w+:|$)/               Genre
/Size: (\d+(?:\.\d)?+\s\w+)/              Size

The (?=,\s\w+:|$) pattern just makes sure it only grabs a value for the "field" (i.e. stops at either the end of the line or the next grouping).

Sign up to request clarification or add additional context in comments.

3 Comments

Could add \b's in the beginning of them. Those $ need /m. Still will be trouble if title is foo, bar: baz.
@Qtax: Indeed, there are a lot of ways I could make this more robust. But, given no real sample to pool from, and not knowing what the end-game was, I figured I'd offer as simple (and working) solution as possible, instead of trying to [forcibly/presumably] redirect down another path.
Thanks, I didn't think to make different matches.
3

This is really more a job for a parser than for Regex. You could split the string on it's spaces and loop through looking for colons and calculating what is what's value that way. Using regex here will be either inefficient or drive you crazy.

Comments

2

Are you able to control the format of the input string? If so, then change it to something like:

Title: some, song title; Length: 5:35; Rating: 0;

Use ; rather then ,. You can the split the string:

string[] parts = input.Split(';');

and work with the parts individually. Don't waste your time with regex in this case.

1 Comment

It comes from accDescription from an IAccessible object, in short no I'm unable to format the string
0

I wouldn't use Regex and instead do

string input = 
    "Title: some, song title; Length: 5:35; Rating: 0; Genre: Other; Size: 7.6 MB";

var values = input.Split(new[] { "; " }, StringSplitOptions.None)
    .Select(v => v.Split(new[] { ": " }, StringSplitOptions.None))
    .ToDictionary(v => v[0], v => v[1]);

I had to change the separator to semicolon instead of comma.

1 Comment

That's hard part as I do not have control of the format of the string
0

Here's one way of doing it without a regex:

dim inputs = {"Title: some, song title, Length: 5:35, Rating: 0, Genre: Other, Size: 7.6 MB", 
              "Size: 7.6 MB, Title: some, song title, Length: 5:35, Genre: Other, Rating: 0" }

for each s in inputs
    dim output as new dictionary(of string, string)
    dim tokens = s.split(", ")
    dim lastKey = ""
    for each t in tokens
        if t.contains(":") then
            dim kv = t.split(":")
            lastKey = kv(0).trim
            output.add(lastkey, "")
            for n = 1 to kv.length - 1
                output(lastkey) &= kv(n) & if( n = kv.length -1, "", ":")
            next n              
        else
            output(lastkey) &= ", " & t.trim
        end if
    next t
next s

This breaks if you have a key that contains a ":".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.