gpt4 book ai didi

C# 正则表达式删除 C 风格注释并提取括号之间的文本

转载 作者:行者123 更新时间:2023-11-28 16:24:54 26 4
gpt4 key购买 nike

问题:

我需要自动从此 JavaScript 中提取所有名称属性(分别适用于大型提供商和小型提供商)

/*
Simple OpenID Plugin
http://code.google.com/p/openid-selector/

This code is licensed under the New BSD License.
*/

var providers_large = {
google : {
name : 'Google',
url : 'https://www.google.com/accounts/o8/id'
},
yahoo : {
name : 'Yahoo',
url : 'http://me.yahoo.com/'
},
aol : {
name : 'AOL',
label : 'Enter your AOL screenname.',
url : 'http://openid.aol.com/{username}'
},
myopenid : {
name : 'MyOpenID',
label : 'Enter your MyOpenID username.',
url : 'http://{username}.myopenid.com/'
},
openid : {
name : 'OpenID',
label : 'Enter your OpenID.',
url : null
}
};

var providers_small = {
livejournal : {
name : 'LiveJournal',
label : 'Enter your Livejournal username.',
url : 'http://{username}.livejournal.com/'
},
/* flickr: {
name: 'Flickr',
label: 'Enter your Flickr username.',
url: 'http://flickr.com/{username}/'
}, */
/* technorati: {
name: 'Technorati',
label: 'Enter your Technorati username.',
url: 'http://technorati.com/people/technorati/{username}/'
}, */
wordpress : {
name : 'Wordpress',
label : 'Enter your Wordpress.com username.',
url : 'http://{username}.wordpress.com/'
},
blogger : {
name : 'Blogger',
label : 'Your Blogger account',
url : 'http://{username}.blogspot.com/'
},
verisign : {
name : 'Verisign',
label : 'Your Verisign username',
url : 'http://{username}.pip.verisignlabs.com/'
},
/* vidoop: {
name: 'Vidoop',
label: 'Your Vidoop username',
url: 'http://{username}.myvidoop.com/'
}, */
/* launchpad: {
name: 'Launchpad',
label: 'Your Launchpad username',
url: 'https://launchpad.net/~{username}'
}, */
claimid : {
name : 'ClaimID',
label : 'Your ClaimID username',
url : 'http://claimid.com/{username}'
},
clickpass : {
name : 'ClickPass',
label : 'Enter your ClickPass username',
url : 'http://clickpass.com/public/{username}'
},
google_profile : {
name : 'Google Profile',
label : 'Enter your Google Profile username',
url : 'http://www.google.com/profiles/{username}'
}
};

openid.locale = 'en';
openid.sprite = 'en'; // reused in german& japan localization
openid.demo_text = 'In client demo mode. Normally would have submitted OpenID:';
openid.signin_text = 'Sign-In';
openid.image_title = 'log in with {provider}';

所以我需要:A) 删除所有 C 风格注释B) 获取 [providers_large,providers_small] 的所有名称值(删除注释后)

到目前为止,我已尝试使用正则表达式删除 C 风格注释(但失败了)和正则表达式获取大括号之间的所有内容(失败)

我随后尝试将其读取为 JSON,但这当然失败了“无效的 json primitve 无论如何”

这是我使用的 stackoverflow 网站这是我迄今为止尝试过的示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleExperiments
{

public class Program
{

// http://stackoverflow.com/questions/2538279/strip-out-c-style-multi-line-comments
// NOT working
static string RemoveCstyleComments(string strInput)
{
string strPattern = @"/[*][\w\d\s]+[*]/";
//strPattern = @"/\*.*?\*/";
strPattern = "/\\*.*?\\*/";

string strOutput = System.Text.RegularExpressions.Regex.Replace(strInput, strPattern, string.Empty, System.Text.RegularExpressions.RegexOptions.Multiline);
Console.WriteLine(strOutput);
return strOutput;
}


// http://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces-i-want-whats-between-the-curly-brace
// http://stackoverflow.com/questions/5337166/regular-expression-get-string-between-curly-braces
// http://stackoverflow.com/questions/1904617/regex-for-removing-curly-brackets-with-nested-curly-brackets
// http://stackoverflow.com/questions/378415/how-do-i-extract-a-string-of-text-that-lies-between-two-brackets-using-net
static string GetCurlyValues(string strInput)
{
string strPattern = "/{(.*?)}/";
strPattern = "/{([^}]*)}/";
strPattern = @"\{(\s*?.*?)*?\}";
strPattern = @"(?<=\{).*(?=\})";
strPattern = "{(.*{(.*)}.*)}";
strPattern = "{{([^}]*)}}";
strPattern = "{{({?}?[^{}])*}}";
strPattern = @"\(([^)]*)\)";

System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(strPattern, System.Text.RegularExpressions.RegexOptions.Multiline);

System.Text.RegularExpressions.Match mMatch = rex.Match(strInput);

foreach (System.Text.RegularExpressions.Group g in mMatch.Groups)
{
Console.WriteLine("Group: " + g.Value);
foreach (System.Text.RegularExpressions.Capture c in g.Captures)
{
Console.WriteLine("Capture: " + c.Value);
}
}

return "";
}


static void ReadFile()
{
try
{
string strFilePath = @"TestFile.txt";
if (System.IO.File.Exists(strFilePath))
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
} // Whend

sr.Close();
} // End Using

} // End if (System.IO.File.Exists(strFilePath))
else
Console.WriteLine("File \"" + strFilePath + "\" does not exist.");
} // End Try
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
} // End Catch

} // End Sub

public class cProvider
{
public string name = "abc";
public string label ="def";
public string url ="url";
}


public class cProviders_large
{
public List<cProvider> foo = new List<cProvider>();
}


static void Main(string[] args)
{
string strContent = System.IO.File.ReadAllText(@"D:\UserName\Downloads\openid-selector-1.3\openid-selector\js\openid-en - Kopie.js.txt");
Console.WriteLine(strContent);
//RemoveCstyleComments(strContent);
//GetCurlyValues(strContent);
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
//object obj = js.DeserializeObject(strContent);

cProviders_large xx = new cProviders_large();
cProvider ap = new cProvider();
xx.foo.Add(ap);
xx.foo.Add(ap);

string res = js.Serialize(xx);
Console.WriteLine(res);


Console.WriteLine(Environment.NewLine);
Console.WriteLine(" --- Press any key to continue --- ");
Console.ReadKey();
} // End Sub Main

} // End Class Program


} // End namespace ConsoleExperiments

任何比我更了解正则表达式的人都可以为我提供必要的正则表达式吗?现在,看起来每次文件更改时我都会手动完成,我真的真的很讨厌这个......

编辑:顺便说一句,v8 包装器使用 C++.NET,因此无法在 Linux 上运行,尽管 v8 引擎在 Linux 上运行得很好。

所以我坚持通过 JSON 转换来解决问题。

最佳答案

您可以使用javascript engine :

using System;
using System.IO;
using Noesis.Javascript;

class Program
{
static void Main()
{
var context = new JavascriptContext();
context.SetParameter("openid", new object());
context.Run(File.ReadAllText("test.js"));
dynamic providers_large = context.GetParameter("providers_large");
foreach (var provider in providers_large)
{
Console.WriteLine(
"name: {0}, url: {1}",
provider.Value["name"],
provider.Value["url"]
);
}
}
}

在我的控制台上打印以下内容:

name: Google, url: https://www.google.com/accounts/o8/id
name: Yahoo, url: http://me.yahoo.com/
name: AOL, url: http://openid.aol.com/{username}
name: MyOpenID, url: http://{username}.myopenid.com/
name: OpenID, url:

关于C# 正则表达式删除 C 风格注释并提取括号之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8323445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com