Regular Expressions: QA Challenge Accepted Conf (March 2015)

16
Regular Expressions … and Their Place in the QA Engineer's Life Svetlin Nakov Manager Training and Inspiration Software University http://softuni.bg

Transcript of Regular Expressions: QA Challenge Accepted Conf (March 2015)

Page 1: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Regular Expressions

… and Their Place in the QA Engineer's Life

Svetlin Nakov

Manager Trainingand Inspiration

Software University

http://softuni.bg

Page 2: Regular Expressions: QA Challenge Accepted Conf (March 2015)

What are Regular Expressions?

• Regular expressions (regex)

– Match text by pattern

• Patterns are defined by special syntax, e.g.

– [0-9]+ matches non-empty sequence of digits

– [A-Z][a-z]* matches a capital + small letters

– \s+ matches whitespace (non-empty)

• \S+ matches non-whitespace

2

Page 3: Regular Expressions: QA Challenge Accepted Conf (March 2015)

3

DEMO

www.regexr.com

Page 4: Regular Expressions: QA Challenge Accepted Conf (March 2015)

More Patterns

• \d+ matches digits

– \D+ matches non-digits

• \w+ matches letters (Unicode)

– \W+ matches non-letters

• \+\d{1,3}([ -]*[0-9]){6,} matches

international phone, e.g. +359 2 123-456

4

Page 5: Regular Expressions: QA Challenge Accepted Conf (March 2015)

5

DEMO

www.regexr.com

Page 6: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Validation by Regex

• ^ matches start of text

• $ matches end of text

• ^\+\d{1,3}([ -]*[0-9]){6,}$ validates

international phone

• +359 2 123-456 is a valid phone

• +359 (888) 123-456 is a invalid phone

6

Page 7: Regular Expressions: QA Challenge Accepted Conf (March 2015)

7

DEMO

www.regexr.com

Page 8: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Regex Literals

• The classical (Perl syntax) is:

– /<regex>/<options>

• Examples:

– /[a-z]+/gi matches all non-empty sequences

of Latin letters, case-insensitively

– /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,20}/gi matches emails (simplified pattern)

8

Page 9: Regular Expressions: QA Challenge Accepted Conf (March 2015)

9

DEMO

www.debuggex.com

Page 10: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Some JS Code: Validation

var emailPattern =

/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,20}$/i;

console.log(emailPattern.test("[email protected]"));

console.log(emailPattern.test("[email protected]"));

console.log(emailPattern.test("invalid@@mail"));

console.log(emailPattern.test("err [email protected]"));

10

Page 11: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Some JS Code: Split

var towns = "Sofia, Varna,Pleven, Veliko Tarnovo; Paris – London––Viena\n\n Пловдив|Каспичан";

console.log(towns.split(/\W+/)); // incorrect

console.log(towns.split(/\s*[.,|;\n\t–]+\s*/));

11

Page 12: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Some JS Code: Match

var text = "I was born at 14-Jun-1980. Today is 14-Mar-2015. Next year starts at 1-Jan-2016 and ends at 31-Dec-2016.";

var dateRegex = /\d{1,2}-\w{3}-\d{4}/gm;

console.log(text.match(dateRegex));

12

Page 13: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Regex for QA Engineers

• Why should a QA understand regex?

– Validation prepare regex for developers

– Write automation testing scripts

• E.g. find a text on the page by regex

– Parse a text, extract info, split data, …

13

Page 14: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Resources

• Play with regex:

– http://www.regexr.com

– https://regex101.com

• Visual regex debugger:

– https://www.debuggex.com

• Regex explained (in Bulgarian)

– http://goo.gl/NbBIPe

–14

Page 15: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Resources (2)

• Interactive Regex Tutorial

– http://regexone.com

• Regex @ W3Schools

– http://w3schools.com/jsref/jsref_obj_regexp.asp

• Regex Tutorial (for Java)– vogella.com/tutorials/JavaRegularExpressions/article.html

15

Page 16: Regular Expressions: QA Challenge Accepted Conf (March 2015)

Regular Expressions

Questions?

16