Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Regular Expressions: QA Challenge Accepted Conf (March 2015)

18,234 views

Published on

Regular Expressions. Validation. Split. Matching, ...
------------------------------------------------------------

Test RegEx at:
http://www.regexr.com

------------------------------------------------------------

[0-9]+

------------------------------------------------------------

[A-Z][a-z]*

------------------------------------------------------------

\s+

------------------------------------------------------------

\S+

------------------------------------------------------------

\w+

------------------------------------------------------------

\W+

------------------------------------------------------------

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

+1-800-555-2468
+359 2 834-2334
+359888123456
(052) 343-434

------------------------------------------------------------

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

+359 2 123-456 is a match
+359 (888) 123-456 is a NOT match

------------------------------------------------------------

Simplified Email Extraction Pattern:
/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,20}/gi

------------------------------------------------------------

var emailPattern =
/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,20}$/i;
console.log(emailPattern.test("test@abv.bg"));
console.log(emailPattern.test("a.hills@gtx.de"));
console.log(emailPattern.test("invalid@@mail"));
console.log(emailPattern.test("err test@abv.bg"));

------------------------------------------------------------

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*/));

------------------------------------------------------------

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));

Published in: Education
  • Be the first to comment

Regular Expressions: QA Challenge Accepted Conf (March 2015)

  1. 1. Regular Expressions … and Their Place in the QA Engineer's Life Svetlin Nakov Manager Training and Inspiration Software University http://softuni.bg
  2. 2. 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
  3. 3. 3 DEMO www.regexr.com
  4. 4. 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
  5. 5. 5 DEMO www.regexr.com
  6. 6. 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
  7. 7. 7 DEMO www.regexr.com
  8. 8. 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
  9. 9. 9 DEMO www.debuggex.com
  10. 10. Some JS Code: Validation var emailPattern = /^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,20}$/i; console.log(emailPattern.test("test@abv.bg")); console.log(emailPattern.test("a.hills@gtx.de")); console.log(emailPattern.test("invalid@@mail")); console.log(emailPattern.test("err test@abv.bg")); 10
  11. 11. Some JS Code: Split var towns = "Sofia, Varna,Pleven, Veliko Tarnovo; Paris – London––Vienann Пловдив|Каспичан"; console.log(towns.split(/W+/)); // incorrect console.log(towns.split(/s*[.,|;nt–]+s*/)); 11
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. Regular Expressions Questions? 16

×