Size: 1681
Comment:
|
Size: 1664
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 51: | Line 51: |
CategoryDyalog | CategoryDyalogDotNetSamples | CategoryDyalogDotNetSamples |
.Net comes with build-in regular expression. Here some useful functions are implement. One is checking an email address for being valid, the other one takes a url and returns "{ProtocolName}:{PortNo}" as a result.
The examples are taken from the C# help file Visual Studio Express comes with. The changes needed to make them work under Dyalog APL/W are not a big deal.
r←IsValidEmailAddr emailAdr;regPattern ⍝⍝ Returns 1 if the string contains a ⍝⍝ valid email address, otherwise 0 ⎕ML ⎕IO←1 ⎕USING,←⊂'' regPattern←'^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$' r←System.Text.RegularExpressions.Regex.IsMatch(emailAdr regPattern)
{{{TestEmailAddr;msg
- msg←{⍵:'Is fine: ',⍺ ⋄ 'Is NOT fine: ',⍺}
{⍵ msg IsValidEmailAddr ⍵}'valid@isfine.com' {⍵ msg IsValidEmailAddr ⍵}'valid@notsofine' {⍵ msg IsValidEmailAddr ⍵}'not$$valid@isfine.com'
}}}
returns
Is fine: ...
Is NOT fine: ...
Is NOT fine: ...
r←ProtocolAndPortFrom url;regPattern;q ⍝⍝ The following code example uses Match.Result to extract a protocol and port number from a URL ⍝⍝ For example, "http://www.contoso.com:8080/letters/readme.html" would return "http:8080". ⎕ML ⎕IO←1 ⎕USING,←⊂'' regPattern←'^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/' r←(System.Text.RegularExpressions.Regex.Match(url regPattern)).Result⊂'${proto}${port}'
TestProtocolAndPort ⎕←ProtocolAndPortFrom'http://www.contoso.com:8080/letters/readme.html'
returns
http:8080