正则表达式帮助和字符串格式 [英] Regex help and string Formatting

查看:87
本文介绍了正则表达式帮助和字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此类作为Javascript最小化器

I have this Class as a Javascript Minimizer

using System.IO;
using System.Net;
using System.Text;
using System.Xml;
    /// <summary>
    /// A C# wrapper around the Google Closure Compiler web service.
    /// </summary>
    public class GoogleClosure
    {
        private static string PostData = "js_code={0}&output_format=xml&output_info=compiled_code&compilation_level=SIMPLE_OPTIMIZATIONS";
        //private static string PostData = "js_code={0}&output_format=xml&output_info=compiled_code&compilation_level=ADVANCED_OPTIMIZATIONS";
        private static string ApiEndpoint = "http://closure-compiler.appspot.com/compile";

        /// <summary>
        /// Compresses the specified file using Google''s Closure Compiler algorithm.
        /// <remarks>
        /// The file to compress must be smaller than 200 kilobytes.
        /// </remarks>
        /// </summary>
        /// <param name="file">The absolute file path to the javascript file to compress.</param>
        /// <returns>A compressed version of the specified JavaScript file.</returns>
        public static string CompressFile(string file)
        {
            string source = File.ReadAllText(file);
            return Compress(source);
        }
        
        public static string Compress(string source)
        {
            XmlDocument xml = CallApi(source);
            return xml.SelectSingleNode("//compiledCode").InnerText;
        }

        /// <summary>
        /// Calls the API with the source file as post data.
        /// </summary>
        /// <param name="source">The content of the source file.</param>
        /// <returns>The Xml response from the Google API.</returns>
        private static XmlDocument CallApi(string source)
        {
            using (WebClient client = new WebClient())
            {
                client.Headers.Add("content-type", "application/x-www-form-urlencoded");
                string data = string.Format(PostData, UrlEncoder.UrlEncode(source));
                string result = client.UploadString(ApiEndpoint, data);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(result);
                return doc;
            }
        }
    }


UrlEncoder类具有UrlEncode(string)和UrlDecode(string)(以及其他所需的私有函数),并且使用Reflector从System.Web的HttpUtility进行了翻录(不确切知道我是否做过违法,但是由于不得不切换到客户端配置文件中的正常.net 4毫无原因:mad :).

然后我通过Google Closure Minimizer在JavaScript中添加了以下文本...


The UrlEncoder Class has UrlEncode(string) and UrlDecode(string) (plus other needed private functions) and it is ripped from HttpUtility in System.Web using Reflector (dont exactly know if i did something illegal but i was pissed from having to switch to normal .net 4 from Client Profile for no reason :mad:).

Then i have the following text in javascript through Google Closure Minimizer...

h="\u0392\u03bb\u03ad\u03c0\u03c9 "+c+"\u03b5\u03b9\u03ba\u03cc\u03bd\u03b5\u03c2",b=true,a;document.getElementById("track2").onclick=d;



您有什么建议,可以让我再次将其重新格式化为utf-8?
我已经尝试过UrlDecode(text.Replace("\\ u0",%u0"));
但会将"+"符号留空.

我想要的是将\ u0392(6个字符)替换为对应的utf8字符(显然不仅是(\ u0392)).



What would you suggest as a way for me to reformat this into utf-8 again?
i have tried UrlDecode(text.Replace("\\u0","%u0"));
but it leaves the "+" symbols blank.

What i want is to replace the \u0392(6 chars) with the corresponding utf8 char (and not only (\u0392) obviously).

推荐答案

请参见 http://msdn.microsoft.com/en-us/library/system.text.encoding .unicode.aspx [ ^ ],了解如何在编码之间进行转换.

干杯

Andi
See http://msdn.microsoft.com/en-us/library/system.text.encoding.unicode.aspx[^] for how to convert between encodings.

Cheers

Andi


第二次尝试:

C#字符始终是unicode,而不是utf8.
您可以将unicode字符串转换为保存utf8字节的字节数组,然后通过二进制流进行存储.

转换为simlpe:

Second attempt:

C# characters are always unicode, not utf8.
You can convert the unicode string into an byte array which holds the utf8 bytes, and then store it by a binary stream.

The conversion is simlpe:

byte[] utf8 = Encoding.UTF8.GetBytes("\u0392\u03bb\u03ad\u03c0\u03c9 Hello This is my text...");



但是我仍然不清楚,您想利用UrlDecode方法转换unicode字符来实现什么.网址中的网址必须经过编码才能形成合法格式.这就是url方法的用途.如果要通过从unicode转换为utf8来减小某些文本的大小,则必须遍历如上所示的字节数组.

干杯

Andi



But it is still not clear to me, what you want to achieve by utilizing the UrlDecode method for converting unicode characters. An url has constrinats that need to be encoded to form a legal pattern. That''s what the url methods are for. If you want to reduce the size of some text by converting from unicode to utf8, then you must go over byte arrays as shown above.

Cheers

Andi


问题是我没有像
byte[] utf8 = Encoding.UTF8.GetBytes("\u0392\u03bb\u03ad\u03c0\u03c9 Hello This is my text...");


这样的字符串 我目前有点:byte [] utf8 = Encoding.UTF8.GetBytes("u0392 u03bb u03ad u03c0 u03c9你好,这是我的文字...");即整个STRING?而不是角色.
像这样的东西:


i currently kinda have:byte[] utf8 = Encoding.UTF8.GetBytes("u0392 u03bb u03ad u03c0 u03c9 Hello This is my text..."); i.e the whole STRING ? and not the character.
Something like:

string result = @"h=""\u0392\u03bb\u03ad\u03c0\u03c9 ""+c+""\u03b5\u03b9\u03ba\u03cc\u03bd\u03b5\u03c2"",b=true,a;document.getElementById(""track2"").onclick=d;";

关于@


这篇关于正则表达式帮助和字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆