开发者

Values from a string using regex (.net)

开发者 https://www.devze.com 2023-03-12 07:35 出处:网络
I\'m attempting to get the values Title: some, song title Length: 5:35 Rating: 0 Genre: Other Size: 7.6 MB

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


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).


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.


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.


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.


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 ":".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号