开发者

Convert C# regex Code to Java

I have found this Regex extractor code in C#. Can someone tell me how this works, and how do I write the equivalent in Java?

// extract songtitle from metadata header. 
// Trim was needed, because some stations don't trim the开发者_Python百科 songtitle
fileName = 
    Regex.Match(metadataHeader, 
      "(StreamTitle=')(.*)(';StreamUrl)").Groups[2].Value.Trim();


This should be what you want.

// Create the Regex pattern
Pattern p = Pattern.compile("(StreamTitle=')(.*)(';StreamUrl)");
// Create a matcher that matches the pattern against your input
Matcher m = p.matcher(metadataHeader);
// if we found a match
if (m.find()) {
    // the filename is the second group. (The `(.*)` part)
    filename = m.group(2);
}


It pulls "MyTitle" from a string such as "StreamTitle='MyTitle';StreamUrl".

The () operators define match groups, there are 3 in your regex. The second one contains the string of interest, and is gotten in the Groups[2].Value.

There's a few very good regex designers out there. The one I use is Rad Software's Regular Expression Designer (www.radsoftware.com.au). It is very useful for figuring out stuff like this (and it uses C# RegEx's).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜