Regex Numeric Range Generator — Regex Tools — 3Widgets
文章推薦指數: 80 %
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
延伸文章資訊
- 1Example: Matching Numeric Ranges with a Regular Expression
The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 t...
- 2regex - Why doesn't [01-12] range work as expected?
Your goal is evidently to specify a number range: any number between 01 and 12 written with two d...
- 36.7. Numbers Within a Certain Range - Regular Expressions ...
You want to match an integer number within a certain range of numbers. You want the regular expre...
- 4Regular Expressions (RegEx) Tutorial #4 - Ranges - YouTube
- 5Regex Numeric Range Generator - Measure Marketer
RegEx Range Generator Tool. RegEx skills are extremely important for people who work on Google An...