Michael Olivero Microsoft Student Ambassador for FIU [email protected] Special Guest: FIU Graduate...
-
Upload
preston-welch -
Category
Documents
-
view
214 -
download
2
Transcript of Michael Olivero Microsoft Student Ambassador for FIU [email protected] Special Guest: FIU Graduate...
Michael OliveroMicrosoft Student Ambassador for [email protected]
Special Guest:FIU Graduate StudentMiguel Gonzalez
Topics Covered TodayTopics Covered Today
Intro to ASP.NETIntro to ASP.NET Regular Expressions (Regex)Regular Expressions (Regex) Web ServicesWeb Services
Brief IntroBrief Intro
Client Server Model (State)Client Server Model (State)
`
`
`
Brief IntroBrief Intro
Client Server Model (State Less)Client Server Model (State Less)
`
`
What happens when you request a web pageWhat happens when you request a web page
`
<html><head><title>My Picture</title></head><body>
My picture is:<img src="images/my.jpg">
</body></html>
Each resource is requested with a separate connection.
The need for dynamic data - The need for dynamic data - CGICGI
`
Issues with CGIIssues with CGI
Multiple ProcessesMultiple Processes Integration with Content creates Integration with Content creates
cluttered codecluttered code Ex: C++ code to write HTML codeEx: C++ code to write HTML code
Come Active Server PagesCome Active Server Pages
Why not mix the code within the HTML page?Why not mix the code within the HTML page?
code
ASP ProcessingASP Processing
`
Issues with ASPIssues with ASP
Parsing overheadParsing overhead Interpretation overheadInterpretation overhead Mixing of presentation & codeMixing of presentation & code All the issues of a non-OOP languageAll the issues of a non-OOP language
No encasulationNo encasulation No inheritanceNo inheritance Etc.Etc.
Come ASPXCome ASPX
Why not create a model from the ground up to Why not create a model from the ground up to resolve all of these issues…resolve all of these issues… Every page is a OOP classEvery page is a OOP class Content is separate from codeContent is separate from code Compiled code not interpretedCompiled code not interpreted InheritanceInheritance ReuseReuse ……and the list goes onand the list goes on
Coming Spring 2004Coming Spring 2004
Quick Demo ASPXQuick Demo ASPX
Topics Covered TodayTopics Covered Today
Intro to ASP.NETIntro to ASP.NET Regular Expressions (Regex)Regular Expressions (Regex) Web ServicesWeb Services
Intro to parsing…Intro to parsing…
How many times have you parsed a How many times have you parsed a string?string? What did you use?What did you use?
Substring()Substring() IndexOf()IndexOf()
Parsing using String class methodsParsing using String class methods
ExampleExample Given an email like Given an email like [email protected]@mike95.com you you
want to pull out the first part of the domain want to pull out the first part of the domain name “mike95”name “mike95”
Sample code using string class methods:Sample code using string class methods:String email = “[email protected]”;String email = “[email protected]”;
String domain = String domain =
email.substring( email.IndexOf(‘@’), email.substring( email.IndexOf(‘@’), email.lastIndexOf(‘.’) – email.IndexOf(‘@’) );email.lastIndexOf(‘.’) – email.IndexOf(‘@’) );
Parsing using RegexParsing using Regex
ExampleExample Given an email like Given an email like [email protected]@mike95.com you you
want to pull out the first part of the domain want to pull out the first part of the domain name “mike95”name “mike95”
Sample code using string class methods:Sample code using string class methods:String email = “[email protected]”;String email = “[email protected]”;
Match m = Match m =
Regex.Match( email, “[^\@]*@([^.]*)” );Regex.Match( email, “[^\@]*@([^.]*)” );
Regular ExpressionsRegular ExpressionsMiguel GonzalezMiguel Gonzalez
What are they? (5-6 min)What are they? (5-6 min) Regex vs Linear String searches (5-6min)Regex vs Linear String searches (5-6min) Regex in .NET (10 min)Regex in .NET (10 min)
ClassesClasses Basic OptionsBasic Options
In-Depth Regex (15 min)In-Depth Regex (15 min) Advanced Regex in .NET (5 min)Advanced Regex in .NET (5 min)
Regex FoundationRegex Foundation What are regular expressions? What are regular expressions?
A powerful technique to process textual dataA powerful technique to process textual data Description of machine that “eats” stringsDescription of machine that “eats” strings
How do they work internally?How do they work internally? DFADFA NFANFA .NET implementation is NFA-based.NET implementation is NFA-based
Examples: Examples: ^from:.*^from:.* .*\b.NET\b.*.*\b.NET\b.* [0-9]+[0-9]+
DFA: Deterministic Finite DFA: Deterministic Finite AutomataAutomata
The machine is deterministic: The machine is deterministic: One line of executionOne line of execution Greedy Little MachineGreedy Little Machine
Always finds the longest match.Always finds the longest match. Tracks all possible matches simultaneouslyTracks all possible matches simultaneously Cannot backtrackCannot backtrack
Cannot remember captured subexpressionsCannot remember captured subexpressions Cannot “look ahead” or “look behind”Cannot “look ahead” or “look behind” Cannot be “lazy”Cannot be “lazy”
Example: Example: Find a price in different coins:Find a price in different coins:
((US|CAN)$|EU)[0-9]+\.[0-9][0-9]((US|CAN)$|EU)[0-9]+\.[0-9][0-9]
$U S
[0-9]
[0-9]
.
[0-9] [0-9]
C
AN
E U
NFA: Non-Deterministic NFA: Non-Deterministic Finite AutomataFinite Automata Warning for the Purists: Not a “real” NFAWarning for the Purists: Not a “real” NFA The machine is non-deterministic: The machine is non-deterministic:
Multiple possible lines of execution Multiple possible lines of execution Has to run through every choice.Has to run through every choice.
Can save points of execution and “backtrack”Can save points of execution and “backtrack” Capture sub-expressionsCapture sub-expressions Greedy, but can be Lazy when it wants toGreedy, but can be Lazy when it wants to
Example: Example: Find a price:Find a price:
((US|CAN)$|EU)[0-9]+\.[0-9][0-9]((US|CAN)$|EU)[0-9]+\.[0-9][0-9]
[0-9]
[0-9]
U
$
Regular Expressions Regular Expressions vs vs
Traditional String MatchingTraditional String Matching EfficiencyEfficiency ConcisenessConciseness SimplicitySimplicity DemoDemo
Regex SyntaxRegex Syntax Character matchingCharacter matching
Literals: Literals: John SmithJohn Smith
Character classesCharacter classes Pre-DefinedPre-Defined
Word Character: \wWord Character: \w Whitespace: \sWhitespace: \s Digits: \dDigits: \d Anything: .Anything: . Negation: \W, \S, \DNegation: \W, \S, \D
User definedUser defined [mnopq][mnopq] Negation: [^mnopq]Negation: [^mnopq]
RangesRanges [a-z] , [0-9][a-z] , [0-9] [0-9a-f][0-9a-f]
DemoDemo
Regex SyntaxRegex Syntax
PositionPosition Start of line: ^Start of line: ^ End of line: $End of line: $ Match a word: \bMatch a word: \b Match a substring: \BMatch a substring: \B
DemoDemo
Regex SyntaxRegex Syntax
QuantifiersQuantifiers Zero or more: *Zero or more: * One or more: +One or more: + Optional (zero or one) : ? Optional (zero or one) : ? Being exact: {n}, {n,} {n,m}Being exact: {n}, {n,} {n,m}
Lazy Quantifiers: Add an extra ? SymbolLazy Quantifiers: Add an extra ? Symbol Example: a*? Example: a*?
DemoDemo
Regex SyntaxRegex Syntax
Grouping expressionsGrouping expressions Unnamed: .*year.*(\d\d\d\d)Unnamed: .*year.*(\d\d\d\d) Named: .*year.*(?<name>\d\d\d\d)Named: .*year.*(?<name>\d\d\d\d)
ORing expressions: ab | cdORing expressions: ab | cd BackreferenceBackreference
By index: \nBy index: \n By name \k<name>By name \k<name>
DemoDemo
Regex in .NETRegex in .NET
.NET Regex implementation.NET Regex implementation NFA-based NFA-based Regex CacheRegex Cache
ClassesClasses System.Text.RegularExpressionsSystem.Text.RegularExpressions
Basic .NET OptionsBasic .NET Options Regex Compilation in .NETRegex Compilation in .NET
Regex ObjectRegex Object
Creating RegexesCreating Regexes new Regex( regex)new Regex( regex) new Regex( regex, options)new Regex( regex, options)
MatchingMatching Regex.isMatch(target)Regex.isMatch(target) Regex.isMatch(target, offset)Regex.isMatch(target, offset) For more complex behavior: Obtain For more complex behavior: Obtain
and use Match objectand use Match object
String Manipulation with RegexString Manipulation with Regex
String replacementString replacement MethodsMethods
Regex.Replace(target, replacement)Regex.Replace(target, replacement) Regex.Replace(target, replacement, count)Regex.Replace(target, replacement, count) Regex.Replace(target, replacement, count, offset)Regex.Replace(target, replacement, count, offset)
Can refer to matches in the regex through escape codes: Can refer to matches in the regex through escape codes: Matched text: $& or $0Matched text: $& or $0 Matched text for Groups: $1 … $N or ${name}Matched text for Groups: $1 … $N or ${name} Text before the match: $`Text before the match: $` Text after the match: $’Text after the match: $’ Original text: $_Original text: $_ Example:Example:
Generate a reply To header from the From header of the original emailGenerate a reply To header from the From header of the original email new Regex(“^From”).Replace(“To:$’”)new Regex(“^From”).Replace(“To:$’”)
String splittingString splitting MethodsMethods
Regex.split(target)Regex.split(target) Regex.split(target, count)Regex.split(target, count) Regex.split(target, count, offset)Regex.split(target, count, offset)
Match ObjectMatch Object Represent the result of applying the RegexRepresent the result of applying the Regex Get a Match Object from RegexGet a Match Object from Regex
Regex.Match(target)Regex.Match(target) Regex.Match(target, offset)Regex.Match(target, offset) Regex.Match(target, offset, maxlength)Regex.Match(target, offset, maxlength)
Get all the matchesGet all the matches Regex.Matches(target)Regex.Matches(target) Regex.Matches(target, offset)Regex.Matches(target, offset)
PropertiesProperties Match.SuccessMatch.Success Match.LengthMatch.Length Match.IndexMatch.Index Match.GroupsMatch.Groups Match.CapturesMatch.Captures
MethodsMethods Match.Result(string)Match.Result(string) Match.NextMatch()Match.NextMatch() Match.Synchronized()Match.Synchronized()
GroupsGroups Represent groupings in the RegexRepresent groupings in the Regex .NET Regex Groups can be named.NET Regex Groups can be named PropertiesProperties
Group.SuccessGroup.Success Group.ValueGroup.Value Group.LengthGroup.Length Group.IndexGroup.Index Group.CapturesGroup.Captures
CapturesCaptures Represent intermediary matchings in the working Represent intermediary matchings in the working
process of the regex/groupprocess of the regex/group PropertiesProperties
Group.SuccessGroup.Success Group.ValueGroup.Value Group.LengthGroup.Length Group.IndexGroup.Index Group.CapturesGroup.Captures
.NET Shortcuts.NET Shortcuts Regex Convenience MethodsRegex Convenience Methods
MatchingMatching Regex.Match(pattern, target)Regex.Match(pattern, target) Regex.Match(pattern, target, options)Regex.Match(pattern, target, options) Regex.IsMatch(pattern, target)Regex.IsMatch(pattern, target) Regex.IsMatch(pattern, target, options)Regex.IsMatch(pattern, target, options) Regex.Matches(pattern, target)Regex.Matches(pattern, target) Regex.Matches(pattern, target, options)Regex.Matches(pattern, target, options)
String ReplacementString Replacement Regex.Replace(pattern, target, replacement)Regex.Replace(pattern, target, replacement) Regex.Replace(pattern, target, replacement, options)Regex.Replace(pattern, target, replacement, options)
String SplittingString Splitting Regex.Split(pattern, target)Regex.Split(pattern, target) Regex.Split(pattern, target, options)Regex.Split(pattern, target, options)
String EscapingString Escaping Regex.Escape(string)Regex.Escape(string) Regex.Unescape(string)Regex.Unescape(string)
Regex Options in .NETRegex Options in .NET
Regex.IgnoreCaseRegex.IgnoreCase Regex.IgnorePatternWhitespaceRegex.IgnorePatternWhitespace Regex.MultilineRegex.Multiline Regex.SinglelineRegex.Singleline Regex.ExplicitCaptureRegex.ExplicitCapture Regex.ECMAScriptRegex.ECMAScript Regex.CompiledRegex.Compiled Regex.RightToLeftRegex.RightToLeft
Regex Compilation in .NETRegex Compilation in .NET
.NET Compilation.NET Compilation Optimizes the NFA for speedOptimizes the NFA for speed Overhead at object creationOverhead at object creation Increased memory cost Increased memory cost
.NET Compilation to Assembly.NET Compilation to Assembly Compiles Regex as an MSIL objectCompiles Regex as an MSIL object Optimizes speed and memoryOptimizes speed and memory
DemoDemo
Topics Covered TodayTopics Covered Today
Intro to ASP.NETIntro to ASP.NET Regular Expressions (Regex)Regular Expressions (Regex) Web ServicesWeb Services