簡體   English   中英

正則表達式長度

[英]Regular expression length

您如何看待使用 looooooong 正則表達式? 這會影響性能嗎?

這是我的正則表達式之一:(塊引用搞砸了,所以我編輯了它)

([(]((((-?((([0-9]+)|([(]-[0-9]+[)]))|(([0-9]*i)|([(]-[0-9]*i[)])))[*/]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)]))[+-]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)][+/*-]([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]|(([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+/*-]((([0-9]*i)|([(]-[0-9]*i[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+/*-]((([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)])))([+/*-](((-?((([0-9]+)|([(]-[0-9]+[)]))|(([0-9]*i)|([(]-[0-9]*i[)])))[*/]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)]))[+-]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)][+/*-]([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]|(([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+/*-]((([0-9]*i)|([(]-[0-9]*i[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+/*-]((([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)])))|(([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))*)|(((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)])([+/*-]((-?((([0-9]+)|([(]-[0-9]+[)]))|(([0-9]*i)|([(]-[0-9]*i[)])))[*/]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)]))[+-]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)][+/*-]([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]|(([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+/*-]((([0-9]*i)|([(]-[0-9]*i[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+/*-]((([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))))+))[)])

我可以使用這個長度的 RegExp 還是不好的做法?

編輯我正在構建復數計算器。 此正則表達式由較短的正則表達式組成。 這就是我的做法:

    var regularExpression = new (function() 
{
    this.re = "(([0-9]+)|([(]-[0-9]+[)]))",
    this.im = "(([0-9]*i)|([(]-[0-9]*i[)]))",
    this.complexNumber = "[(]((-?"+ this.re + "[+-]" + this.im + ")|(-?" + this.im + "[+-]" + this.re +"))[)]",
    this.expression = "("+
                                                "(-?("+this.re+"|"+this.im+")[*/]("+this.im+"|"+this.re+"))|"+
                                                "(-?"+this.re+"[+-]"+this.im+"[+-]("+this.im+"|"+this.re+"))|"+
                                                "("+this.complexNumber+"[+/*-]("+this.complexNumber+"|"+this.im+"|"+this.re+"))|"+
                                                "(-?"+this.im+"[+/*-](" + this.im+"|"+this.complexNumber+"))|"+
                                                "(-?"+this.re+"[+/*-](" + this.re+"|"+this.complexNumber+"))"+
                                        ")",
    this.simpleExpression = "([(]("+
                                                        "("+this.expression+"([+/*-]("+this.expression+"|"+this.im+"|"+this.re+"|"+this.complexNumber+"))*)|"+
                                                        "(("+this.im+"|"+this.re+"|"+this.complexNumber+")([+/*-]"+this.expression+")+)"+
                                                    ")[)])"
})();

所以我的regularExpression.simpleExpression 更長,因為它使用了幾次表達式。 這就是為什么我要問和擔心。

simpleExpression 正在尋找像這樣的字符串:(12321-12231i+1233123*(12i-231)+12323-i)

正則表達式非常快,當它們以良好的方式編寫時(絕對比基於條件的代碼快得多)。

但是,您應該問自己的問題是代碼是否可維護和可讀——將來可能有人必須重寫這部分代碼,甚至一段時間后您可能第一眼也看不懂。

從這里開始的第一點可能會回答您的問題: https : //hackernoon.com/few-simple-rules-for-good-coding-my-15-years-experience-96cb29d4acd9

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM