Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 A Quick Introduction to Regular Expressions in Java.
Regular expression. Validation need a hard and very complex programming. Sometimes it looks easy but actually it is not. So there is a lot of time and.
Characters and Strings. Representation of single characters Data type char is the data type that represents single characters, such as letters, numerals,
Scripting Languages Chapter 8 More About Regular Expressions.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Regular Expressions. String Matching The problem of finding a string that “looks kind of like …” is common  e.g. finding useful delimiters in a file,
Applications of Regular Expressions BY— NIKHIL KUMAR KATTE 1.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Science: Text and Language Dr Andy Evans. Text analysis Processing of text. Natural language processing and statistics.
Last Updated March 2006 Slide 1 Regular Expressions.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9 Characters and Strings.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Using Regular Expressions in Java for Data Validation Evelyn Brannock Jan 30, 2009.
PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,
Regular Expressions in Perl Part I Alan Gold. Basic syntax =~ is the matching operator !~ is the negated matching operator // are the default delimiters.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Regular Expression in Java 101 COMP204 Source: Sun tutorial, …
Regular Expressions.
Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 1 Regular Expressions and String processing Animated Version.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
BY Sandeep Kumar Gampa.. What is Regular Expression? Regex in.NET Regex Language Elements Examples Regular Expression API How to Test regex in.NET Conclusion.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
VBScript Session 13.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
C# Strings 1 C# Regular Expressions CNS 3260 C#.NET Software Development.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Regular Expressions. Overview Regular expressions allow you to do complex searches within text documents. Examples: Search 8-K filings for restatements.
Module 6 – Generics Module 7 – Regular Expressions.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
Python for NLP Regular Expressions CS1573: AI Application Development, Spring 2003 (modified from Steven Bird’s notes)
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 5 Regular Expressions.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Regular Expressions.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
University of Central Florida COP 3330 Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Selenium WebDriver Web Test Tool Training
Matcher functions boolean find() Attempts to find the next subsequence of the input sequence that matches the pattern. boolean lookingAt() Attempts to.
Presentation transcript:

Sadegh Aliakbary

Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Strings String Regular Expressions String Formatting Mutable Strings StringBuffer StringBuilder 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Search in Strings indexOf() method 4JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

regionMatches() regionMatches(offset, str, strOffset, length) A substring of this String object is compared to a substring of the argument other String str1 = "Salam. Bye. See You Later"; String str2 = "Hi! Bye!"; //returns true: boolean b = str1.regionMatches(7, str2, 4, 3); 5JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Regular Expressions (1) ^exprexpr at beginning of line. expr$expr at end of line..any single character (except newline) [xyz] either x, y, or z. [p-z] a range: any character from p to z. [p-z1-9] either any character from p to z or any digit from 1 to 9 [^p-z] ‘^’ as first character inside a bracket negates the pattern. it matches any character except characters p to z. Xy Matches X followed by y. x | y either x or y. 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Regular Expressions (2) \d digits (equivalent to [0–9]). \D non-digits \wword characters ([a-zA-Z_0-9]) \W non-word characters \s whitespaces (equivalent to [ \t\n\x0B\f\r]) \S non-whitespaces \A beginning of string \Z end of string 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Regular Expressions (3) expr? 0 or 1 occurrence of expr (expr{0,1}) expr* 0 or more occurrences of expr (expr{0,}). expr+ 1 or more occurrences of expr (expr{1,}) expr{x} x occurrences of expr. expr{x, y} between x and y occurrences of expr. expr{x,} x or more occurrences of expr. 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Pattern Examples National code boolean b = code.matches("\\d{10}"); expression (not perfect) String mail = String Regex = "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*" + + "(\\.[A-Za-z]{2,})"; boolean b = mail.matches( Regex); 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Pattern and Matcher Classes String str = "Ali's and "+ "Taghi's and "; //Note to parentheses: String regex = Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.print("FirstName="+matcher.group(1)); System.out.print(", LastName="+matcher.group(2)); System.out.print(", Domain="+matcher.group(3)); System.out.println(", Ext="+matcher.group(4)); } 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Replace Patterns String str = and also " + // Note to parentheses: String regex = Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); String safe = matcher.replaceAll( "$1 DOT $2 [AT] $3 DOT $4"); Safe  ali DOT alavi [AT] gmail DOT com and also taghi DOT taghavi [AT] chmail DOT ir 11JAVACUP.ir

Format Strings String formatted = String.format("%d %.2f", 5, 2.123); System.out.println(formatted); System.out.printf("%d %5.2f", 12, 2.123); %b Boolean %c Character %f Floating point numer in decimal format %s String %t Date/time … 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

What is wrong with this code: String = "ali"; +="."; +="alavi"; +="gmail"; +="."; +="com"; System.out.println( ); 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Mutable Strings StringBuilder buffer = new StringBuilder("ali"); buffer.append(".").append("alavi"); buffer.append("gmail").append(".com"); System.out.println(buffer.toString()); StringBuffer: thread-safe StringBuilder: NOT thread-safe 14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source15