matlab regular expression n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Matlab Regular Expression PowerPoint Presentation
Download Presentation
Matlab Regular Expression

Loading in 2 Seconds...

play fullscreen
1 / 27

Matlab Regular Expression - PowerPoint PPT Presentation


  • 125 Views
  • Uploaded on

Matlab Regular Expression. 2012.4.09. Let’s begin with ‘*’. Asterisk ‘*’ can match all words when we interact with program, e.g. MS office software. ‘*.*’ is match all files name. Reg ular Exp ression -- regexp.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Matlab Regular Expression' - shawn


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
let s begin with
Let’s begin with ‘*’
  • Asterisk ‘*’ can match all words when we interact with program, e.g. MS office software.
  • ‘*.*’ is match all files name.
reg ular exp ression regexp
Regular Expression -- regexp
  • In computing, a regular expression provides a concise and flexible means to "match" (specify and recognize) strings of text, such as particular characters, words, or patterns of characters. -- From Wikipedia
matlab regular expression1
Matlab Regular Expression
  • regexp: Match regular expression (case sensitive)
  • regexpi: Match regular expression (case insensitive)
  • regexprep: Replace string using regular expression
examples
Examples

str= 'bat cat can car COAT court cut ct CAT-scan';

regexpi(str, 'c[aeiou]+t')

ans =

5 17 28 35

  • [aeiou]: 1 char within [] should be matched;
  • +: matches the preceding pattern element 1+ times;
  • defalut output: starting indices of each match.
slide6

str = 'Madrid, Spain';

s1 = regexp(str, '[A-Z]');

s2 = regexp(str, '\s');

s1 =

1 9

s2 =

8

  • [A-Z]: a char from A to Z of alphabet should be contained;
  • \s: space characters, matches a whitespace character.
slide7

str = 'regexp helps you relax';

[m s e] = regexp(str, '\w*x\w*', 'match', 'start', 'end')

m =

'regexp' 'relax'

s =

1 18

e =

6 22

  • match, start, end: specifies the output format, text, starting and ending indices of matched words;
  • \w: space characters, same as [A-Za-z0-9_]
  • *: matches the preceding pattern element 0+ times.
slide9

str = 'She sells sea shells by the seashore.';

[matchstrsplitstr] = regexp(str, '[Ss]h.', 'match', 'split')

matchstr=

'She' 'she' 'sho'

splitstr =

'' ' sells sea ' 'lls by the sea' 're.'

  • split: specifies the output format, find the substrings delimited by the pattern.
slide11

str = 'The rain in Spain falls mainly on the plain.';

expr = '..ain';

matchStr = regexp(str, expr, 'match')

matchStr =

' rain' 'Spain' ' main' 'plain‘

[mat idx] = regexp(str, '[rpm]ain', 'match', 'start')

mat =

'rain' 'pain' 'main'

idx =

5 14 25

[mat ix1 ix2] = regexp(str, '\w*n\s', 'match', 'start', 'end')

mat =

'rain ' 'in ' 'Spain ' 'on '

ix1 =

5 10 13 32

ix2 =

9 12 18 34

slide16

regexp('B5 A2 6F 63 R6 P4 B2 BC', '(?:[A-Z]\d\s?){2,}', 'match')

ans =

'B5 A2 ' 'R6 P4 B2 ‘

regexpi(pstr, '(let|tel)\w+', 'match')

ans =

'lets' 'telegram'

introduction to using tokens
Introduction to Using Tokens

poestr = ['While I nodded, nearly napping, ' 'suddenly there came a tapping,'];

[mat tokext] = regexp(poestr, '(\S)\1', 'match', 'tokens', 'tokenExtents');

mat =

'dd' 'pp' 'dd' 'pp'

The tokens returned in cell array tok are:

'd', 'p', 'd', 'p‘

Starting and ending indices for each token in the input string poestr are:

11 11, 26 26, 35 35, 57 57

using tokens in a replacement string
Using Tokens in a Replacement String

poestr= ['While I nodded, nearly napping, ' 'suddenly there came a tapping,'];

regexp(poestr, '(?<anychar>.)\k<anychar>', 'match')

ans=

'dd' 'pp' 'dd' 'pp‘

When referencing a named token within the expression, use the syntax \k<name> instead of the numeric \1, \2, etc.:

using tokens in a replacement string1
Using Tokens in a Replacement String

regexprep('NormaJeanBaker', '(\w+\s\w+)\s(\w+)', '$2, $1')

ans =

Baker, Norma Jean

Cstr = 'Whose woods these are I think I know.' ;

s = regexprep(cstr, '(.)\1', '--', 'ignorecase')

s =

'Whose w--ds these are I think I know.‘

ss='Hello <a href="world">world</a>. 2 < 5';

b='<.*?>';

sr=regexprep(ss,b,'')

sr =

'Hello world. 2 < 5'

slide22

Named tokens are denoted by the pattern (?<name>...). The 'names' result structure will have fields corresponding to the named tokens in EXPRESSION

str = 'John Davis; Rogers, James';

pat = '(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)';

n = regexp(str, pat, 'names');

n(1).first =

'John'

n(1).last =

'Davis'

n(2).first =

'James'

n(2).last =

'Rogers'

slide23
When one of STRING or EXPRESSION is a cell array of strings, REGEXP matches the string input with each element of the cell array input

str= {'Madrid, Spain' 'Romeo and Juliet' 'MATLAB is great'};

pat = '\s';

regexp(str, pat)

ans =

{[8]; [6 10]; [7 10]}

slide24

When both STRING and EXPRESSION are cell arrays of strings, REGEXP matches the elements of STRING and EXPRESSION sequentially. The number of elements in STRING and EXPRESSION must be identical

str= {'Madrid, Spain' 'Romeo and Juliet' 'MATLAB is great'};

pat = {'\s', '\w+', '[A-Z]'};

regexp(str, pat)

ans =

{[8]; [1 7 11]; [1 2 3 4 5 6]}

slide27

http://www.mathworks.com/help/techdoc/matlab_prog/f0-42649.htmlhttp://www.mathworks.com/help/techdoc/matlab_prog/f0-42649.html

http://www.mathworks.com/help/techdoc/ref/regexp.html