Regular Expressions - Pages supplied by users
文章推薦指數: 80 %
REGEX BASICS ; Matching simple expressions, Most characters match themselves. The only exceptions are called special characters: asterisk ( * ),; plus sign ( + ) ...
REGULAREXPRESSIONS
Multi-threadedNewsWatcher
Contents
RegularExpressions
Whatarethey?
Moreinformation
Regexpbasics
Matchingsimpleexpressions
Matchinganycharacter
Repeatingexpressions
Groupingexpressions
Choosingonecharacterfrommany
Matchingthebeginningorendofaline
Regexpextensions
Matchingwords
Alternatives
Someusefulexamples
Gobacktothepageonfiltering.
Seethepageoncreatingandeditingfilters.
REGULAREXPRESSIONS
Whatareregularexpressions?
Regularexpressionsareasystemformatchingpatternsintextdata,which
arewidelyusedinUNIXsystems,andoccasionallyonpersonalcomputers
aswell.Theyprovideaverypowerful,butalsoratherobtuse,setoftools
forfindingparticularwordsorcombinationsofcharactersinstrings.
Onfirstreading,thisallseemsparticularlycomplicatedandnotofmuch
useoverandabovethestandardstringmatchingprovidedintheEdit
Filtersdialog(Wordmatching,forexample).Inactualfact,inthese
casesNewsWatcherconvertsyourstringmatchingcriteriaintoaregular
expressionwhenapplyingfilterstoarticles.
However,youcanusesomeofthesimplermatchingcriteriawithease
(someexamplesaresuggestedbelow),andgraduallybuildupthecomplexity
oftheregularexpressionsthatyouuse.
Onepointtonoteisthatregularexpressionsarenotwildcards.The
regularexpression'c*t'doesnotmean'match"cat","cot"'etc.
Inthiscase,itmeans'matchzeroormore'c'charactersfollowedby
at',soitwouldmatch't','ct','cccct'etc.
Informationsources
Theinformationhereisanamalgamationofthedocumentationofregular
expressionsintheMetrowerksCodeWarriorIDE,andofachapterinthe
bookUNIXPowerTools(Peek,O'Reilly&Loukides).Onlineinformation
(oftenthemanpagesforUNIXutilities)isavailablebyusingone
ofthesearchengines(e.g.InfoSeek)to
searchfor'regularexpressions'.
REGEXBASICS
Matchingsimpleexpressions
Mostcharactersmatchthemselves.Theonlyexceptionsarecalled
specialcharacters:
asterisk(*),
plussign(+),
questionmark(?),
backslash(\),
period(.),
caret(^),
squarebrackets([and]),
dollarsign($),
ampersand(&).
orsign(|).
Tomatchaspecialcharacter,precedeitwithabackslash,likethis\*.
Forexample,
Thisexpression...matchesthis...butnotthis...
aab
\.\*.*dog
100100ABCDEFG
Matchinganycharacter
Aperiod(.)matchesanycharacterexceptanewlinecharacter.
Thisexpression...matchesthis...butnotthis...
.artdartart
carthurt
tartdark
Repeatingexpressions
Youcanrepeatexpressionswithanasteriskorplussign.
Aregularexpressionfollowedbyanasterisk(*)matcheszeroormore
occurrencesoftheregularexpression.Ifthereisanychoice,the
firstmatchingstringinalineisused.
Aregularexpressionfollowedbyaplussign(+)matchesoneormore
occurrencesoftheone-characterregularexpression.Ifthereisany
choice,thefirstmatchingstringinalineisused.
Aregularexpressionfollowedbyaquestionmark(?)matcheszeroor
oneoccurrenceoftheone-characterregularexpression.
Forexample:
Thisexpression... matchesthis...butnotthis...
a+b ab b
aaab baa
a*b b daa
ab
aaab
.*cat cat dog
9393cat
theoldcat
c7sb@#puiercat
a[n]?h aherb annhat
anherb
Sotomatchanyseriesofzeroormorecharacters,use".*".On
itsownthisisn'tmuchuse,butinthemiddleofalongerregular
expression,itcanbe.
Groupingexpressions
Ifanexpressionisenclosedinparentheses((and)),theeditor
treatsitasoneexpressionandappliesanyasterisk(*)orplus(+)
tothewholeexpression.
Forexample
Thisexpression... matchesthis...butnotthis...
(ab)*c abc ababab
ababababc ababd
(.a)+b xab b
ra5afab aagb
Choosingonecharacterfrommany
Astringofcharactersenclosedinsquarebrackets([])matchesanyonecharacter
inthatstring.Ifthefirstcharacterinthebracketsisacaret(^),itmatches
anycharacterexceptthoseinthestring.Forexample,[abc]matchesa,b,orc,
butnotx,y,orz.However,[^abc]matchesx,y,orz,butnota,b,orc.
Aminussign(-)withinsquarebracketsindicatesarangeofconsecutiveASCII
characters.Forexample,[0-9]isthesameas[0123456789].Theminussignloses
itsspecialmeaningifit'sthefirst(afteraninitial^,ifany)orlastcharacter
inthestring.
Ifarightsquarebracketisimmediatelyafteraleftsquarebracket,itdoes
notterminatethestringbutisconsideredtobeoneofthecharacterstomatch.
Ifanyspecialcharacter,suchasbackslash(\),asterisk(*),orplussign(+),
isimmediatelyaftertheleftsquarebracket,itdoesn'thaveitsspecialmeaning
andisconsideredtobeoneofthecharacterstomatch.
Thisexpression... matchesthis...butnotthis...
[aeiou][0-9] a6 ex
i3 9a
u2 $6
[^cfl]og dog cog
bog fog
END[.] END. END;
ENDDO
ENDIAN
Matchingthebeginningorendofaline
Youcanspecifythataregularexpressionmatchonlythebeginningorendoftheline.
InNewsWatcher,alineisthewholefieldthatisbeingmatched,forexamplethe
authororsubjectfields.Thesearecalledanchorcharacters:
Ifacaret(^)isatthebeginningoftheentireregularexpression,
itmatchesthebeginningofaline.
Ifadollarsign($)isattheendoftheentireregularexpression,
itmatchestheendofaline.
Ifanentireregularexpressionisenclosedbyacaretanddollarsign
(^likethis$),itmatchesanentireline.
Thisexpression... matchesthis...butnotthis...
^(thecat).+ thecatruns seethecatrun
.+(thecat)$ watchthecat thecateats
So,tomatchallstringscontainingjustonecharacters,use"^.$".
REGEXEXTENSIONS
Matchingwords
Youcanspecifythataregularexpressionmatchpartsofwordswith\<
(matchthestartofaword)and\>(matchtheendofaword).
Anexpressionlike"\
延伸文章資訊
- 1116 - Stack Overflow
The regular expression for this is really simple. Just use a character class. The hyphen is a spe...
- 2Metacharacters - IBM
- 3Simple RegEx Tutorial
Regular Expression can be used in Content Filter conditions. ... These symbols indicate the start...
- 4Regex symbol list and regex examples - Codexpedia
Regex symbol list and regex examples ; \b Backslash and b, matches a word boundary. For example, ...
- 5Regular Expression (Regex) Tutorial
A range expression consists of two characters separated by a hyphen ( - ). It matches any single ...