Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expressions ICCM 2017

Similar presentations


Presentation on theme: "Regular Expressions ICCM 2017"— Presentation transcript:

1 Regular Expressions ICCM 2017

2 Regular Expression A pattern of special characters used to match strings in a search Typically made up from special characters called metacharacters Regular expressions are used across platforms: UNIX Commands: grep, ed C#, Java Powershell XML Schemas

3 Metacharacters any non-metacharacter matches itself RE Metacharacter
. Any one character, except new line [a-z] Any one of the enclosed characters (e.g. a-z) * Zero or more of preceding character ? or \? Zero or one of the preceding characters + or \+ One or more of the preceding characters any non-metacharacter matches itself

4 The grep Utility “grep” command: searches for text in file(s)
Examples: % grep root mail.log % grep r..t mail.log % grep ro*t mail.log % grep ‘ro*t’ mail.log % grep ‘r[a-z]*t’ mail.log

5 more Metacharacters RE Metacharacter Matches… ^ beginning of line $
end of line \char Escape the meaning of char following it [^] One character not in the set \< Beginning of word anchor \> End of word anchor ( ) or \( \) Tags matched characters to be used later (max = 9) | or \| Or grouping x\{m\} Repetition of character x, m times (x,m = integer) x\{m,\} Repetition of character x, at least m times x\{m,n\} Repetition of character x between m and m times

6 An atom specifies what text is to be matched and where
it is to be found.

7 A single character matches itself
Single Character Atom A single character matches itself

8 matches any single character except for a new
Dot Atom matches any single character except for a new line character (\n)

9 Class Atom matches only single character that can be any of
the characters defined in a set: Example: [ABC] matches either A, B, or C. Notes: 1) A range of characters is indicated by a dash, e.g. [A-Q] 2) Can specify characters to be excluded from the set, e.g. [^0-9] matches any character other than a number.

10 Examples

11 Anchors Anchors tell where the next character in the pattern must
be located in the text data.

12 KNime Example

13 Powershell Examples -match operator compares a string to a regex and returns true or false "Calvin College" -match "lvin“ -replace does as it says, it will replace an expression with another value "Calvin College" -replace "Calvin", "Hope“ Select-String helps filter output Select-String -Pattern "555" -Path .\nameList.txt | ForEach-Object { $_ -Replace "555","888"}

14 C# using System; using System.Text.RegularExpressions; class Program {
static void Main() { Regex regex = new Match match = regex.Match("Dot 55 Perls"); if (match.Success) { Console.WriteLine(match.Value); } }

15 XML – From the IRS <xsd:simpleType name="AmountType">
<xsd:annotation> </xsd:annotation> <xsd:restriction base="xsd:string"> <xsd:maxLength value="19"/> <xsd:pattern value="([0-9]+\.[0-9][0-9]?)"/> </xsd:restriction> </xsd:simpleType>


Download ppt "Regular Expressions ICCM 2017"

Similar presentations


Ads by Google