Java Regular Expressions Zero-length assertions, back-references, quantifiers, and more Anubhava Srivastava BIRMINGHAM - MUMBAI < html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> Java Regular Expressions Copyright © 2017 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: July 2017 Production reference: 1210717 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78728-870-6 www.packtpub.com Credits Author Copy Editor Anubhava Srivastava Muktikant Garimella Reviewer Project Coordinator Peter Verhas Ulhas Kambali Commissioning Editor Proofreader Merint Thomas Mathew Safis Editing Acquisition Editor Indexer Nitin Dasan Francy Puthiry Content Development Editor Graphics Vikas Tiwari Abhinash Sahu Technical Editor Production Coordinator Jijo Maliyekal Melwyn Dsa About the Author Anubhava Srivastava has more than 22 years of professional experience in architecture, design, and software development He specializes mainly in the J2EE technology stack, web technologies, CICD, test-driven development, and AWS cloud-based development He is an active contributor to the Stack Overflow community He currently stands in the top 35 users in terms of reputations out of the 7.5 million Stack Overflow users He is passionate about regular expressions and is always willing to sharpen his regex skills further He has answered close to 10,000 regex questions on Stack Overflow He has authored and released a test-driven framework, called GenericFixture, for FitNesse as an open source software He maintains a technical blog at http:/ /anubhava.wordpress.com Anubhava can be found on LinkedIn at https://www.linkedin.com/in/anubhava/ You can follow him on Twitter at @anubhava First, I would like to express my gratitude toward the whole editing and for (int j = i; j > 0; j ) { sb1.append('a'); sb2.append("a?"); } sb2.append(sb1); final Pattern p = Pattern.compile("^" + sb2.toString() + "$"); Matcher m = p.matcher(sb1.toString()); long start = System.nanoTime(); m.matches(); long end = System.nanoTime(); System.out.printf("%s:: ( %sms ) :: Pattern , Input %n", i, (end - start)/1_000_000, sb2, sb1); } } } When you compile and run the preceding program and look at the generated output, you will note an output as follows: 1:: ( 0ms ) :: Pattern , Input 2:: ( 0ms ) :: Pattern , Input 3:: ( 0ms ) :: Pattern , Input 4:: ( 0ms ) :: Pattern , Input 5:: ( 0ms ) :: Pattern , Input 6:: ( 0ms ) :: Pattern , Input 7:: ( 0ms ) :: Pattern , Input 8:: ( 0ms ) :: Pattern , Input 9:: ( 0ms ) :: Pattern , Input 10:: ( 0ms ) :: Pattern , Input 11:: ( 0ms ) :: Pattern , Input 12:: ( 0ms ) :: Pattern , Input 13:: ( 10ms ) :: Pattern , Input 14:: ( 1ms ) :: Pattern , Input 15:: ( 15ms ) :: Pattern , Input 16:: ( 18ms ) :: Pattern , Input