Vous êtes sur la page 1sur 5

4/18/2017 VBScriptRegularExpressions

VBScriptRegularExpressions
Advertisements

PreviousPage NextPage

WhatareRegularExpressions?
RegularExpressionsisasequenceofcharactersthatformsapattern,whichismainlyusedforsearchandreplace.Thepurposeofcreatinga
patternistomatchspecificstrings,sothatthedevelopercanextractcharactersbasedonconditionsandreplacecertaincharacters.

RegExpObject
RegExpobjecthelpsthedeveloperstomatchthepatternofstringsandthepropertiesandmethodshelpustoworkwithRegularExpressions
easily.ItissimilartoRegExpinJavaScript

Properties
PatternThePatternmethodrepresentsastringthatisusedtodefinetheregularexpressionanditshouldbesetbeforeusingthe
regularexpressionobject.

IgnoreCaseABooleanpropertythatrepresentsiftheregularexpressionshouldbetestedagainstallpossiblematchesinastringif
trueorfalse.Ifnotspecifiedexplicitly,IgnoreCasevalueissettoFalse.

GlobalABooleanpropertythatrepresentsiftheregularexpressionshouldbetestedagainstallpossiblematchesinastring.Ifnot
specifiedexplicitly,GlobalvalueissettoFalse.

Methods
Test(searchstring)TheTestmethodtakesastringasitsargumentandreturnsTrueiftheregularexpressioncansuccessfullybe
matchedagainstthestring,otherwiseFalseisreturned.

Replace(searchstring, replacestring) The Replace method takes 2 parameters. If the search is successful then it replaces that
matchwiththereplacestring,andthenewstringisreturned.Iftherearenomatchesthentheoriginalsearchstringisreturned.

Execute(searchstring) The Execute method works like Replace, except that it returns a Matches collection object, containing a
Matchobjectforeachsuccessfulmatch.Itdoesn'tmodifytheoriginalstring.

MatchesCollectionObject
TheMatchescollectionobjectisreturnedasaresultoftheExecutemethod.ThiscollectionobjectcancontainzeroormoreMatchobjectsand
thepropertiesofthisobjectarereadonly.

CountTheCountmethodrepresentsthenumberofmatchobjectsinthecollection.

ItemTheItemmethodenablesthematchobjectstobeaccessedfrommatchescollectionsobject.

MatchObject
TheMatchobjectiscontainedwithinthematchescollectionobject.Theseobjectsrepresentthesuccessfulmatchafterthesearchforastring.

FirstIndexItrepresentsthepositionwithintheoriginalstringwherethematchoccurred.Thisindexarezerobasedwhichmeans
thatthefirstpositioninastringis0.

LengthAvaluethatrepresentsthetotallengthofthematchedstring.

ValueAvaluethatrepresentsthematchedvalueortext.ItisalsothedefaultvaluewhenaccessingtheMatchobject.

https://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm 1/5
4/18/2017 VBScriptRegularExpressions

AllaboutPatternParameter
ThepatternbuildingissimilartoPERL.PatternbuildingisthemostimportantthingwhileworkingwithRegularExpressions.Inthissection,we
willdealwithhowtocreateapatternbasedonvariousfactors.

PositionMatching
Thesignificanceofpositionmatchingistoensurethatweplacetheregularexpressionsatthecorrectplaces.

Symbol Description

^ Matchesonlythebeginningofastring.

$ Matchonlytheendofastring.

\b Matchesanywordboundary

\B Matchesanynonwordboundary

LiteralsMatching
Anyformofcharacterssuchasalphabet,numberorspecialcharacterorevendecimal,hexadecimalcanbetreatedasaLiteral.Sincefewof
the characters have already got a special meaning within the context of Regular Expression, we need to escape them using escape
sequences.

Symbol Description

Alphanumeric Matchesalphabeticalandnumericalcharactersonly.

\n Matchesanewline.

\[ Matches[literalonly

\] Matches]literalonly

\( Matches(literalonly

\) Matches)literalonly

\t Matcheshorizontaltab

\v Matchesverticaltab

\| Matches|literalonly

\{ Matches{literalonly

\} Matches}literalonly

\\ Matches\literalonly

\? Matches?literalonly

\* Matches*literalonly

\+ Matches+literalonly

\. Matches.literalonly

\b Matchesanywordboundary

\B Matchesanynonwordboundary

\f Matchesaformfeed

\r Matchescarriagereturn

\xxx MatchestheASCIIcharacterofanoctalnumberxxx.

\xdd MatchestheASCIIcharacterofanhexadecimalnumberdd.

\uxxxx MatchestheASCIIcharacterofanUNICODEliteralxxxx.

CharacterClassesMatching

https://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm 2/5
4/18/2017 VBScriptRegularExpressions

ThecharacterclassesarethePatternformedbycustomizedgroupingandenclosedwithin[]braces.Ifweareexpectingacharacterclass
thatshouldnotbeinthelist,thenweshouldignorethatparticularcharacterclassusingthenegativesymobol,whichisacap^.

Symbol Description

[xyz] Matchanyofthecharacterclassenclosedwithinthecharacterset.

[^xyz] MatchesanyofthecharacterclassthatareNOTenclosedwithinthecharacterset.

. Matchesanycharacterclassexcept\n

\w Matchanywordcharacterclass.Equivalentto[azAZ_09]

\W Matchanynonwordcharacterclass.Equivalentto[^azAZ_09]

\d Matchanydigitclass.Equivalentto[09].

\D Matchanynondigitcharacterclass.Equivalentto[^09].

\s Matchanyspacecharacterclass.Equivalentto[\t\r\n\v\f]

\S Matchanyspacecharacterclass.Equivalentto[^\t\r\n\v\f]

RepetitionMatching
Repetitionmatchingallowsmultiplesearcheswithintheregularexpression.Italsospecifiesthenumberoftimesanelementisrepeatedina
RegularExpression.

Symbol Description

* MatcheszeroormoreoccurrencesofthegivenregularExpression.Equivalentto{0,}.

+ MatchesoneormoreoccurrencesofthegivenregularExpression.Equivalentto{1,}.

? MatcheszerooroneoccurrencesofthegivenregularExpression.Equivalentto{0,1}.

{x} Matchesexactlyxnumberofoccurrencesofthegivenregularexpression.

{x,} Matchatleastxormoreoccurrencesofthegivenregularexpression.

{x,y} Matchesxtoynumberofoccurencesofthegivenregularexpression.

Alternation&Grouping
Alternation and grouping helps developers to create more complex Regular Expressions in particularly handling intricate clauses within a
RegularExpressionwhichgivesagreatflexibilityandcontrol.

Symbol Description

0 Groupingaclausetocreateaclause."(xy)?(z)"matches"xyz"or"z".

| Alternationcombinesoneregularexpressionclauseandthenmatchesanyoftheindividualclauses."(ij)|(23)|
(pq)"matches"ij"or"23"or"pq".

BuildingRegularExpressions
Belowarefewexamples,whichclearlyexplainonhowtobuildaRegularExpression.

RegularExpression Description

"^\s*.."and"..\s*$" Representsthattherecanbeanynumberofleadingandtrailingspacecharactersinasingleline.

"((\$\s?)|(#\s?))?" Representsanoptional$or#signfollowedbyanoptionalspace.

"((\d+(\.(\d\d)?)?))" Representsthatatleastonedigitispresentfollowedbyanoptionaldecimalsandtwodigitsafterdecimals.

Example
Thebelowexamplecheckswhetherornottheuserenteredanemailidwhoseformatshouldmatchsuchthatthereisanemailidfollowedby
'@'andthenfollowedbydomainname.

https://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm 3/5
4/18/2017 VBScriptRegularExpressions

<!DOCTYPEhtml>
<html>
<body>
<scriptlanguage="vbscript"type="text/vbscript">
strid="welcome.user@tutorialspoint.co.us"
Setre=NewRegExp
Withre
.Pattern="^[\w\.]{1,}\@([\dazAZ]{1,}\.){1,}[\dazAZ]{2,3}$"
.IgnoreCase=False
.Global=False
EndWith

'TestmethodreturnsTRUEifamatchisfound
Ifre.Test(strid)Then
Document.write(strid&"isavalidemailaddress")
Else
Document.write(strid&"isNOTavalidemailaddress")
EndIf

Setre=Nothing
</script>
</body>
</html>

PreviousPage NextPage

Advertisements

Write for us FAQ's Helping Contact


Copyright 2017. All Rights Reserved.

Enter email for newsletter go

https://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm 4/5
4/18/2017 VBScriptRegularExpressions

https://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm 5/5

Vous aimerez peut-être aussi