How to Troubleshoot Apps for the Modern Connected Worker
Kodar-lan - Text and editors Regular expression
1. Tim Gremalm
Developer at Conmel Data AB
Programming, Web, Databases, Windows/Linux, Servers,
Network, TCP/IP, Robotics, Electronics
tim@gremalm.se
http://tim.gremalm.se
Lecture - Regular expression (Tim Gremalm)
2. Regular expression
●
A string that forms a searchpattern
●
The basics of regex started in the 1950's
●
Used in many UNIX-tools like grep, ed, awk
●
Most programming languages supports regular expressions
●
Also called regex, regexp
Lecture - Regular expression (Tim Gremalm)
3. Common uses of regular expression
●
Match a text with a searchpattern for validation
●
Search replace (substitute) text
●
Syntax-highlighting in editors
●
Extracting relevant parts of a text
Lecture - Regular expression (Tim Gremalm)
4. Regular expression
Match literary
Special Characters
Carriage return +
Tab +
v
Vertical tab +
f
Form feed +
a
Alarm
[b]
Backspace
e
Escape
N{name}
Lecture - Regular expression (Tim Gremalm)
New line +
t
Just enter you text like:
yoursearchterm
n
r
●
Escape Character +
Named Character
5. Regular expression
Anchors
●
Position your search term
Anchors
^
End of string +
Word boundary +
Not word boundary +
Start of word
>
Lecture - Regular expression (Tim Gremalm)
End of line +
<
bStartOfWord
$
B
●
Start of string +
b
^StartOfLine
A
Z
●
Start of line +
End of word
6. Regular expression
Character Classes
Character Classes
●
Specify a [class] to search for
Control character
s
White space
S
Not white space
d
Digit
D
Not digit
w
Word
W
Not word
xhh
Hexadecimal character hh
Oxxx
Lecture - Regular expression (Tim Gremalm)
c
Octal character xxx
7. Regular expression
Quantifiers
Quantifiers
*
Lecture - Regular expression (Tim Gremalm)
1 or more, ungreedy +
0 or 1 +
0 or 1, ungreedy +
Exactly 3 +
3 or more +
3, 4 or 5 +
{3,5}?
l*
+?
{3,5}
●
1 or more +
{3,}
[d]+
+
{3}
●
0 or more, ungreedy +
??
Specify how many times a class may occur
*?
?
●
0 or more +
3, 4 or 5, ungreedy +
8. Regular expression
Ranges
Ranges
.
Lecture - Regular expression (Tim Gremalm)
[abc]
Range (a or b or c) +
Not a or b or c +
Letter between a and q +
Upper case letter +
between A and Q +
Digit between 0 and 7 +
n
[a-zA-Z]
Passive Group +
[0-7]
●
Group +
[A-Q]
[A-E]
(...)
[a-q]
●
a or b +
[^abc]
Specify a class even further
(a|b)
(?:...)
●
Any character except new line (n) +
nth group/subpattern +