Regex Numeric Range Generator — Regex Tools — 3Widgets

文章推薦指數: 80 %
投票人數:10人

Generate regular expressions for numeric ranges. Just enter start and end of the range and get a valid regex. Works for both positive and negative numbers. RegexNumericRangeGenerator RangeBegin RangeEnd Makeleadingzerosoptional Useshorthand(\d)fordigits([0-9]) GeneratedRegularExpression Copytoclipboard NumericRangesinRegularExpressions Regularexpressionsarepatternsusedtomatchcharactercombinationsinstrings.Thechallengeisthatregexdoesn'tknowwhatanumberis.Inotherwords,aregexdoesn'tknowthat1-10meansanynumberfrom1to10.Insteadregexgoesthroughthestringcharacterbycharacterandchecksifitmatchesthepattern. Sowhynotjustconvertastringtointbeforecomparison?Insomecasesyouneedtofindnumbersinsidestringsandregularexpressionisoftenanoptionthatperformswell.TheremightbealsoproductssuchasGoogleAnalyticsthatformatcustomdatavaluesasstrings.YouneedtouseregexinGoogleAnalyticsforexampleinordertofilternumericvaluesincustomdimensions. Withregexyouhaveacoupleofoptionstomatchadigit.Youcanuseanumberfrom0to9tomatchasinglechoice.Oryoucanmatcharangeofdigitswithacharactergroupe.g.[4-9].Ifthecharactergroupallowsanydigit(i.e.[0-9]),itcanbereplacedwithashorthand(\d). Inordertomatchamulti-digitnumericrangesinregex,youneedtoapproachtheproblembysplittingitintosmallerchunks.Theeasieswaytodothisistostartbysplittingtherangebasedonhowmanydigitseachnumberhas. E.g.forrange3-13youcanhavetwogroupsa)3-9andb)10-13.Groupa)hasasingledigitandcanbematchedsimplywithregex'[3-9]',whilegroupb)canhastwodigits.Thefirstdigitis1andtheseconddigit0-3.Sotheregexforgroupb)wouldbe'1[0-3]'.Thefinalregexsimplycombinesthesetwowiththealternationoperator(|)resultingin'(3-9|1[0-3])'(i.e.3-9OR1[0-3]) Example:RegexNumberRange1-20 Range1-20hasbothsingledigitnumbers(1-9)andtwodigitnumbers(10-20).Fordoubledigitnumberswehavetosplitthegroupintwo10-19(orinregex:"1[0-9]")and20.Thenwecanjoinallthesewithanalternationoperatortoget"([1-9]|1[0-9]|20)". Example:RegexNumberRange1-100 Range1-100hasone,twoandthreedigitnumbers.Soit'ssimilarto1-20,butnowourtwodigitnumbersare10-99(orinregex:"[1-9][0-9]").Andthere'sonlyonethreedigitnumber"100".Againwecanjointhesewiththealternationoperatortoget"([1-9]|[1-9][0-9]|100)". Example:RegexNumberRange0-255 Againwehaveone,twoandthreedigitnumbers.Nowoursingledigitnumbersare0-9,twodigitnumbersare10-99("[1-9][0-9]"),butthreedigitnumbersare100-255.Thethreedigitnumbersneedtobesplitfutherinto100-199("1[0-9][0-9]"orinshort"1[0-9]{2}")and200-255,andthelatterfurtherinto200-249("2[0-4][0-9]")and250-255("25[0-5]").Combiningallthesewiththealternationoperatorresultsin:"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])". Resources to-regex-rangeNode.jspackage ThisRegexNumericRangeGeneratorisbuiltwithJavaScriptusinganopensourceto-regex-rangepackage.Itisafunctionthattakestwointegers:theminvalueandmaxvalue(formattedasstringsornumbers)andreturnsavalidregularexpression. NumericRangesinRegular-Expressions.info Agreatresourceofinformationaboutregularexpressionnumberranges.Itexplainstheminaneasy-to-readformatwithgoodexamples. RegexforNumbersandNumberRangeatRegexTutorial.org AtutorialthatteachesyouhowtomatchnumbersandnumberrangeinRegularexpressions. Recommendedservices ManagedHostingfordevelopersstarting$9.50permonth.—Opalstack



請為這篇文章評分?