简体   繁体   English

将C#regex代码转换为Java

[英]Convert C# regex Code to Java

I have found this Regex extractor code in C#. 我在C#中找到了这个正则表达式提取器代码。 Can someone tell me how this works, and how do I write the equivalent in Java? 有人可以告诉我这是如何工作的,我如何用Java编写等价的东西?

// extract songtitle from metadata header. 
// Trim was needed, because some stations don't trim the 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". 它从诸如“StreamTitle ='MyTitle'; StreamUrl”之类的字符串中提取“MyTitle”。

The () operators define match groups, there are 3 in your regex. ()运算符定义匹配组,正则表达式中有3个。 The second one contains the string of interest, and is gotten in the Groups[2].Value. 第二个包含感兴趣的字符串,并在组[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). 我使用的是Rad Software的正则表达式设计器(www.radsoftware.com.au)。 It is very useful for figuring out stuff like this (and it uses C# RegEx's). 这对于找出这样的东西非常有用(它使用C#RegEx)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM