Sie sind auf Seite 1von 5

9/23/2016

TclRegularExpressions

TclRegularExpressions
Advertisements

PreviousPage

NextPage

The"regexp"commandisusedtomatcharegularexpressioninTcl.Aregularexpressionis
asequenceofcharactersthatcontainsasearchpattern.Itconsistsofmultiplerulesandthe
followingtableexplainstheserulesandcorrespondinguse.
S.No.
1

Rule&Description

x
Exactmatch.

[az]
Anylowercaseletterfromaz.

.
Anycharacter.

^
Beginningstringshouldmatch.

$
Endingstringshouldmatch.

\^

https://www.tutorialspoint.com/tcltk/tcl_regular_expressions.htm

1/5

9/23/2016

TclRegularExpressions

Backlash sequence to match special character ^.Similarly you can use for other
characters.
7

()
Addtheabovesequencesinsideparenthesistomakearegularexpression.

x*
Shouldmatch0ormoreoccurrencesoftheprecedingx.

x+
Shouldmatch1ormoreoccurrencesoftheprecedingx.

10

[az]?
Shouldmatch0or1occurrenceoftheprecedingx.

11

{digit}
Matches exactly digit occurrences of previous regex expression. Digit that
contains09.

12

{digit,}
Matches 3 or more digit occurrences of previous regex expression. Digit that
contains09.

13

{digit1,digit2}
Occurrencesmatchestherangebetweendigit1anddigit2occurrencesofprevious
regexexpression.

Syntax
Thesyntaxforregexisgivenbelow
regexpoptionalSwitchespatternssearchStringfullMatchsubMatch1...subMatchn

https://www.tutorialspoint.com/tcltk/tcl_regular_expressions.htm

2/5

9/23/2016

TclRegularExpressions

Here, regex is the command. We will see about optional switches later. Patterns are the
rules as mentioned earlier. Search string is the actual string on which the regex is
performed.Fullmatchisanyvariabletoholdtheresultofmatchedregexresult.Submatch1
toSubMatchnareoptionalsubMatchvariablethatholdstheresultofsubmatchpatterns.
Let'slookatsomesimpleexamplesbeforedivingintocomplexones.Asimpleexamplefor
astringwithanyalphabets.Whenanyothercharacterisencounteredtheregex,searchwill
bestoppedandreturned.
#!/usr/bin/tclsh
regexp{([AZ,az]*)}"TclTutorial"ab
puts"FullMatch:$a"
puts"SubMatch1:$b"

Whentheabovecodeisexecuted,itproducesthefollowingresult
FullMatch:Tcl
SubMatch1:Tcl

MultiplePatterns
Thefollowingexampleshowshowtosearchformultiplepatterns.Thisisexamplepattern
foranyalphabetsfollowedbyanycharacterfollowedbyanyalphabets.
#!/usr/bin/tclsh
regexp{([AZ,az]*).([AZ,az]*)}"TclTutorial"abc
puts"FullMatch:$a"
puts"SubMatch1:$b"
puts"SubMatch2:$c"

Whentheabovecodeisexecuted,itproducesthefollowingresult
FullMatch:TclTutorial
SubMatch1:Tcl
SubMatch2:Tutorial

A modified version of the above code to show that a sub pattern can contain multiple
patternsisshownbelow
#!/usr/bin/tclsh
regexp{([AZ,az]*.([AZ,az]*))}"TclTutorial"abc
puts"FullMatch:$a"
puts"SubMatch1:$b"
puts"SubMatch2:$c"

Whentheabovecodeisexecuted,itproducesthefollowingresult
https://www.tutorialspoint.com/tcltk/tcl_regular_expressions.htm

3/5

9/23/2016

TclRegularExpressions

FullMatch:TclTutorial
SubMatch1:TclTutorial
SubMatch2:Tutorial

SwitchesforRegexCommand
ThelistofswitchesavailableinTclare,
nocaseUsedtoignorecase.
indicesStorelocationofmatchedsubpatternsinsteadofmatchedcharacters.
lineNewlinesensitivematching.Ignoresthecharactersafternewline.
startindexSetstheoffsetofstartofsearchpattern.
Markstheendofswitches
Intheaboveexamples,Ihavedeliberatelyused[AZ,az]forallalphabets,youcaneasily
usenocaseinsteadofasshownbelow
#!/usr/bin/tclsh
regexpnocase{([AZ]*.([AZ]*))}"TclTutorial"abc
puts"FullMatch:$a"
puts"SubMatch1:$b"
puts"SubMatch2:$c"

Whentheabovecodeisexecuted,itproducesthefollowingresult
FullMatch:TclTutorial
SubMatch1:TclTutorial
SubMatch2:Tutorial

Anotherexampleusingswitchesisshownbelow
#!/usr/bin/tclsh
regexpnocaseline{([AZ]*.([AZ]*))}"Tcl\nTutorial"ab
puts"FullMatch:$a"
puts"SubMatch1:$b"
regexpnocasestart4line{([AZ]*.([AZ]*))}"Tcl\nTutorial"ab
puts"FullMatch:$a"
puts"SubMatch1:$b"

Whentheabovecodeisexecuted,itproducesthefollowingresult
FullMatch:Tcl
SubMatch1:Tcl

https://www.tutorialspoint.com/tcltk/tcl_regular_expressions.htm

4/5

9/23/2016

TclRegularExpressions

FullMatch:Tutorial
SubMatch1:Tutorial

PreviousPage

NextPage

Advertisements

Write for us

FAQ's

Helping

Contact

Copyright 2016. All Rights Reserved.


Enter email for newsletter

https://www.tutorialspoint.com/tcltk/tcl_regular_expressions.htm

go

5/5

Das könnte Ihnen auch gefallen