Http://blog.sina.com.cn/s/blog_53f29119010009uf.html
Regular expressions The word goes to college and listens to a guy in the same dorm--that guy's a made, and now he's gone--until yesterday, and I think of something else. Just looked at the time thought a little bit of content a morning on the fixed, looked for a while, just more and more feel hair dizzy, eye hair green-also really TMD difficult ~ ~. See today only found that the original book is too indiscriminate, hehe ... However, it is really a bit of space to write a regular expression. ~ ~ First Contact Regular expression, there is the wrong place also please play the heroes more correct Ah! ~
Regular Expressionsis an expression (also a string of characters) that defines a string pattern. Using regular expressions, you can find and replace large sections of text in a complex way.
There are three regular expression functions provided by Matlab:
regexp--is used to find the string;
regexpi--is used to find strings, but is not case-sensitive;
The regexprep--is used to find and replace strings.
Today isThe first part--the match of a single character
Let's start with the simple--take the REGEXPI function as an example. Suppose you want to search for a string containing the character ' cat ', and the regular expression for the search is ' cat '. If the search is not sensitive to case, the word ' catalog ', ' Catherine ', ' sophisticated ' can match. Other words:
Regular expression: ' Cat '
Matches: ' Cat ', ' catalog ', ' Catherine ', ' sophisticated '
This seems to be the same thing that we usually get out of ctrl+f in Notepad, huh ... (BTW: In order to be convenient, the following narrative in the string and the regular expression "is omitted not to write.) )
1 Period Symbol'. '--match any one (only one) character (including spaces).
Suppose you are playing English Scrabble and want to find three-letter words, and the words must start with the ' t ' letter and end with the ' n ' letter. Also, if you have an English dictionary, you can use regular expressions to search for all of its contents. To construct this regular expression, you can use a wildcard character--the period symbol '. In this way, the complete expression is ' T.N ', which matches ' tan ', ' ten ', ' Tin ' and ' ton ', also matches ' t#n ', ' TPN ' and even ' t n ', there are many other meaningless combinations. This is because the period symbol matches all characters, including spaces:
Regular expression: T.N
Matches: ten, tin, Ton, T N, TPN, t#n, [email protected]
MATLAB Example Program:
Clear;clc
Str= ' Ten,&8yn2tin6ui>&ton, T n,-356tpn,$$$ $t #n,[email Protected])., [email Protected]&ny ';
pat= ' T.N ';
O1=REGEXPI (Str,pat, ' start ')% uses the ' start ' parameter to specify the starting position of the output O1 as a substring that matches the regular expression
O2=REGEXPI (Str,pat, ' end ')% specifies the end position of the output O1 as a substring that matches the regular expression with the ' start ' parameter
O3=REGEXPI (Str,pat, ' match ')% specifies the output O2 as a substring that matches the regular expression with the ' match ' parameter
[O11,o22,o33]=regexpi (Str,pat, ' Start ', ' End ', ' match ')% outputs both the starting position and the substring
The output is:
O22 =
3 8 13 18 23 28 33 36
O33 =
' Ten ' tin ' ton ' t n ' TPN ' t#n ' [email protected] ' t&n '
O1 =
1 10 18 23 31 39 48 51
O2 =
3 12 20 25 33 41 50 53
O3 =
' Ten ' tin ' ton ' t n ' TPN ' t#n ' [email protected] ' t&n '
O11 =
1 10 18 23 31 39 48 51
O22 =
3 12 20 25 33 41 50 53
O33 =
' Ten ' tin ' ton ' t n ' TPN ' t#n ' [email protected] ' t&n '
2 square brackets Symbol' [Oum] '--find any one of the square brackets is a match
In order to solve the problem that the period symbol matching range is too wide, you can specify the characters that appear to be meaningful in square brackets (' [] '). At this point, only the characters specified inside the square brackets characters participate in the match. In other words, the regular expression ' t[aeio]n ' only matches ' tan ', ' Ten ', ' Tin ' and ' ton '. But ' Toon ', ' Taen ' does not match, because within square brackets you can only match a single character:
Regular expression: T[aeio]n
Matching: Tan, ten, tin, ton
MATLAB Example Program:
Clear;clc
Str= ' Ten,&8yn2tin6ui>&ton, T n,-356tpn,$$$ $t #n,[email Protected])., [email Protected]&ny ';
pat= ' T[aeiou]n ';
O1=REGEXPI (Str,pat, ' start ')% uses the ' start ' parameter to specify the starting position of the output O1 as a substring that matches the regular expression
O2=REGEXPI (Str,pat, ' end ')% specifies the end position of the output O1 as a substring that matches the regular expression with the ' start ' parameter
O3=REGEXPI (Str,pat, ' match ')% specifies the output O2 as a substring that matches the regular expression with the ' match ' parameter
[O11,o22,o33]=regexpi (Str,pat, ' Start ', ' End ', ' match ')% outputs both the starting position and the substring
The output result is
O1 =
1 10 18
O2 =
3 12 20
O3 =
' Ten ' tin ' ton '
O11 =
1 10 18
O22 =
3 12 20
O33 =
' Ten ' tin ' ton '
3 connector in square brackets ' [C1-C2] '--matches any one of the alphabetic sequences (in alphabetical order) from the beginning of the character C1 to the end of the character C2.
In order to solve the problem that the period symbol matching range is too wide, you can specify the characters that appear to be meaningful in square brackets (' [] '). At this point, only the characters specified inside the square brackets characters participate in the match. In other words, the regular expression ' t[aeio]n ' matches only ' tan ', ' Ten ', ' Tin ' and ' ton '. But ' Toon ' does not match, because within the square brackets you can only match a single character:
Regular expression: T[a-z]n
Matching: Tan, Tbn,tcn,tdn,ten,..., txn, TYN,TZN
MATLAB Example Program:
Clear;clc
Str= ' Ten,&8yn2tin6ui>&ton, T n,-356tpn,$$$ $t #n,[email Protected])., [email Protected]&ny ';
pat= ' T[a-z]n ';
O1=REGEXPI (Str,pat, ' start ')% uses the ' start ' parameter to specify the starting position of the output O1 as a substring that matches the regular expression
O2=REGEXPI (Str,pat, ' end ')% specifies the end position of the output O1 as a substring that matches the regular expression with the ' start ' parameter
O3=REGEXPI (Str,pat, ' match ')% specifies the output O2 as a substring that matches the regular expression with the ' match ' parameter
[O11,o22,o33]=regexpi (Str,pat, ' Start ', ' End ', ' match ')% outputs both the starting position and the substring
4 \n--Special characters
is a single character that is guided by ' \ ' and represents a special meaning or cannot be entered directly. When using the printf function output we often use ' \ n ' instead of a carriage return, and here is the same reason to use \ n to represent a carriage return in a regular expression. Similar to the \ t horizontal tab, ' \* ' means ' * ' and so on. The latter case is used in queries that have grammatical characters in the regular expression. See the table below.
Here are some of the escaped characters that match a single character regular the expression and the value that matches it.
\xn or \x{n} matches characters with an octal value of N
\on or \o{n} matches characters with a hexadecimal value of N
\a Alarm (beep)
\b Backspace
\ t Horizontal tab
\ n New Line
\v Vertical Tab
\f Page Break
\ r return character
\e Escape
\c some characters c which have grammatical function or special meaning in regular expression, should be matched by \c, and cannot be directly matched with C
Examples of MATLAB programs
Clear;clc
Str= ' l.[a-c]i.$.a ';
Pat1= '. '; Pat2= ' \. ';
O=REGEXPI (STR,PAT1, ' match ')
O1=REGEXPI (STR,PAT2, ' match ')
The output is:
o =
' l '. ' ' [' A '-'-' C '] ' I '. ' ' $ '. ' A
O1 =
‘.‘ ‘.‘ ‘.‘
5 \w,\s and \d---range expressions
Unlike the escape characters in the \ n table above, \w,\s,\d matches not a particular character, but a certain class of characters. Specify the following:
\w matches any single literal character, equivalent to [a-za-z0-9_];
\s matches any single white-space character, equivalent to [\t\f\n\r];
\d matches any single number, equivalent to [0-9];
\s matches any single character except whitespace, which is equivalent to the ^ in ^\t\f\n\r]--square brackets;
\w matches any single character, equivalent to [^a-za-z0-9_];
\d matches any single character outside the divisor character, which is equivalent to [^0-9].
Examples of MATLAB programs, which are quoted here are examples of MATLAB help:
Str= ' Easy as a ";% this string can really be a bit of a meaning, hehe
pat= ' \d ';
[O1,o2]=regexpi (Str,pat, ' Start ', ' match ')
The output is:
O1 =
9 11 13
O2 =
' 1 ' 2 ' 3 '
MATLAB Regular Expression (i) (GO)