際際滷

際際滷Share a Scribd company logo
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, checking for
valid user input, filtering email, 
 Regular expressions are a common tool for
this
 most languages support regular expressions
 in Java, they can be used to describe valid
delimiters for Scanner (and other places)
Matching
 When you give a regular expression (a regex
for short) you can check a string to see if it
matches that pattern
 e.g. Suppose that we have a regular
expression to describe a comma then maybe
some whitespace delimiters
 The string , would match that expression. So
would ,  and , n
 But these wouldnt:  , ,,  word
Note
 The finite state machines and regular
languages from MACM 101 are closely
related
 they describe the same sets of characters that
can be matched with regular expressions
 (Regular expression implementations are
sometimes extended to do more than the regular
language definition)
Basics
 When we specified a delimiter
new Scanner().useDelimiter(,);
  the , is actually interpreted as a regular
expression
 Most characters in a regex are used to
indicate that character must be right here
 e.g. the regex abc matches only one string:
abc
 literal translation: an a followed by a b followed
by a c
Repetition
 You can specify this character repeated
some number of times in a regular
expression
 e.g. match wot or woot or wooot 
 A * says match zero or more of those
 A + says match one or more of those
 e.g. the regex wo+t will match the strings above
 literal translation: a w followed by one or more
os followed by a t
Example
 Read a text file, using comma and any
number of spaces as the delimiter
Scanner filein = new Scanner(
new File(file.txt)
).useDelimiter(, *);
while(filein.hasNext())
{
System.out.printf((%s), filein.next());
}
a comma followed by
zero or more spaces
Character Classes
 In our example, we need to be able to match
any one of the whitespace characters
 In a regular expression, several characters
can be enclosed in []
 that will match any one of those characters
 e.g. regex a[123][45]will match these:
a14 a15 a24 a25 a34 a35
 An a; followed by a 1,2, or 3; followed by 4
or 5
Example
 Read values, separated by comma, and one
whitespace character:
Scanner filein = new Scanner()
.useDelimiter(,[ nt]);
 Whitespace technically refers to some other
characters, but these are the most common:
space, newline, tab
 java.lang.Character contains the real
definition of whitespace
Example
 We can combine this with repetition to get the
right version
 a comma, followed by some (optional) whitespace
Scanner filein = new Scanner()
.useDelimiter(,[ nt]*);
 The regex matches a comma followed by
zero or more spaces, newlines, or tabs.
 exactly what we are looking for
More Character Classes
 A character range can be specified
 e.g. [0-9] will match any digit
 A character class can also be negated, to
indicate any character except
 done by inserting a ^ at the start
 e.g.[^0-9] will match anything except a digit
 e.g.[^ nt] will match any non-whitespace
Built-in Classes
 Several character classes are predefined, for
common sets of characters
 . (period): any character
 d : any digit
 s : any space
 p{Lower} : any lower case letter
 These often vary from language to language.
 period is universal, s is common, p{Lower} is
Java-specific (usually its [:lower:])
Examples
 [A-Z] [a-z]*
 title case words (Title, I :not word or AB)
 p{Upper}p{Lower}*
 same as previous
 [0-9].*
 a digit, followed by anything (5q, 2345, 2)
 gr[ea]y
 grey or gray
Other Regex Tricks
 Grouping: parens can group chunks together
 e.g. (ab)+ matches ab or abab or ababab
 e.g. ([abc] *)+ matches a or a b c, abc 
 Optional parts: the question mark
 e.g. ab?c matches only abc and ac
 e.g. a(bc+)?d matches ad, abcd, abcccd,
but not abd or accccd
  and many more options as well
Other Uses
 Regular expressions can be used for much
more than describing delimiters
 The Pattern class (in java.util.regex)
contains Javas regular expression
implementation
 it contains static functions that let you do simple
regular expression manipulation
  and you can create Pattern objects that do
more
In a Scanner
 Besides separating tokens, a regex can be
used to validate a token when its read
 by using the .next(regex) method
 if the next token matches regex, it is returned
 InputMismatchException is thrown if not
 This allows you to quickly make sure the
input is in the right form.
  and ensures you dont continue with invalid
(possibly dangerous) input
Example
Scanner userin = new Scanner(System.in);
String word;
System.out.println(Enter a word:);
try{
word = userin.next([A-Za-z]+);
System.out.printf(
That word has %d letters.n,
word.length() );
} catch(Exception e){
System.out.println(That wasnt a word);
}
Simple String Checking
 The matches function in Pattern takes a
regex and a string to try to match
 returns a boolean: true if string matches
 e.g. in previous example could be done
without an exception:
word = userin.next();
if(matches([A-Za-z]+, word)) {  // a word
}
else{  // give error message
}
Compiling a Regex
 When you match against a regex, the pattern
must first be analyzed
 the library does some processing to turn it into
some more-efficient internal format
 it compiles the regular expression
 It would be inefficient to do this many times
with the same expression
Compiling a Regex
 If a regex is going to be used many times, it
can be compiled, creating a Pattern object
 it is only compiled when the object is created, but
can be used to match many times
 The function Pattern.compile(regex)
returns a new Pattern object
Example
Scanner userin = new Scanner(System.in);
Pattern isWord = Pattern.compile([A-Za-z]+);
Matcher m;
String word;
System.out.println(Enter some words:);
do{
word = userin.next();
m = isWord.matcher(word);
if(m.matches() ) {  // a word
} else {  // not a word
}
} while(!word.equals(done) );
Matchers
 The Matcher object that is created by
patternObj.matcher(str) can do a lot
more than just match the whole string
 give the part of the string that actually matched
the expression
 find substrings that matched parts of the regex
 replace all matches with a new string
 Very useful in programs that do heavy string
manipulation

More Related Content

Similar to Regex Experession with Regex functions o (20)

FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raj Gupta
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
Ahmed El-Arabawy
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
James Armes
Understanding Regular expressions: Programming Historian Study Group, Univers...
Understanding Regular expressions: Programming Historian Study Group, Univers...Understanding Regular expressions: Programming Historian Study Group, Univers...
Understanding Regular expressions: Programming Historian Study Group, Univers...
Allison Jai O'Dell
Regex startup
Regex startupRegex startup
Regex startup
PayPal
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
Sandy Smith
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
wayn
2.regular expressions
2.regular expressions2.regular expressions
2.regular expressions
Praveen Gorantla
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
JSregularExpressions.pptx
JSregularExpressions.pptxJSregularExpressions.pptx
JSregularExpressions.pptx
MattMarino13
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenit (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenit (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenit (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenit (BMR G...
Andrea Telatin
Regular Expressions(Theory of programming languages))
Regular Expressions(Theory of programming languages))Regular Expressions(Theory of programming languages))
Regular Expressions(Theory of programming languages))
khudabux1998
2013 - Andrei Zmievski: Cl鱈nica Regex
2013 - Andrei Zmievski: Cl鱈nica Regex2013 - Andrei Zmievski: Cl鱈nica Regex
2013 - Andrei Zmievski: Cl鱈nica Regex
PHP Conference Argentina
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017
Sandy Smith
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5
bilcorry
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
Sandy Smith
Pythonintro
PythonintroPythonintro
Pythonintro
Hardik Malhotra
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raj Gupta
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
Ahmed El-Arabawy
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
James Armes
Understanding Regular expressions: Programming Historian Study Group, Univers...
Understanding Regular expressions: Programming Historian Study Group, Univers...Understanding Regular expressions: Programming Historian Study Group, Univers...
Understanding Regular expressions: Programming Historian Study Group, Univers...
Allison Jai O'Dell
Regex startup
Regex startupRegex startup
Regex startup
PayPal
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
Sandy Smith
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
wayn
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
JSregularExpressions.pptx
JSregularExpressions.pptxJSregularExpressions.pptx
JSregularExpressions.pptx
MattMarino13
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenit (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenit (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenit (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenit (BMR G...
Andrea Telatin
Regular Expressions(Theory of programming languages))
Regular Expressions(Theory of programming languages))Regular Expressions(Theory of programming languages))
Regular Expressions(Theory of programming languages))
khudabux1998
2013 - Andrei Zmievski: Cl鱈nica Regex
2013 - Andrei Zmievski: Cl鱈nica Regex2013 - Andrei Zmievski: Cl鱈nica Regex
2013 - Andrei Zmievski: Cl鱈nica Regex
PHP Conference Argentina
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017
Sandy Smith
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5
bilcorry
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
Sandy Smith
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge

Recently uploaded (20)

Unit No. 4 - Immunopharmacologyslides.pptx
Unit No. 4 - Immunopharmacologyslides.pptxUnit No. 4 - Immunopharmacologyslides.pptx
Unit No. 4 - Immunopharmacologyslides.pptx
Ashish Umale
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
Prabhakar Singh Patel
BUSINESS QUIZ | THE QUIZ CLUB OF PSGCAS | 17TH MARCH 2025 .pptx
BUSINESS QUIZ | THE QUIZ CLUB OF PSGCAS | 17TH MARCH 2025 .pptxBUSINESS QUIZ | THE QUIZ CLUB OF PSGCAS | 17TH MARCH 2025 .pptx
BUSINESS QUIZ | THE QUIZ CLUB OF PSGCAS | 17TH MARCH 2025 .pptx
Quiz Club of PSG College of Arts & Science
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptxA-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
Quiz Club of PSG College of Arts & Science
Unit No 4- Chemotherapy of Malignancy.pptx
Unit No  4- Chemotherapy of Malignancy.pptxUnit No  4- Chemotherapy of Malignancy.pptx
Unit No 4- Chemotherapy of Malignancy.pptx
Ashish Umale
UNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptxUNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptx
HARIHARAN A
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. DabhadeCombinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. DabhadeIntroduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
McElaney "What is inclusive publishing and why do we care about accessibility...
McElaney "What is inclusive publishing and why do we care about accessibility...McElaney "What is inclusive publishing and why do we care about accessibility...
McElaney "What is inclusive publishing and why do we care about accessibility...
National Information Standards Organization (NISO)
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptxBIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
maniramkumar
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷sPurchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Celine George
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
Nguyen Thanh Tu Collection
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
A Biodiction : A Unit of Dr. Divya Sharma
Proteins, Bio similars & Antibodies.pptx
Proteins, Bio similars &  Antibodies.pptxProteins, Bio similars &  Antibodies.pptx
Proteins, Bio similars & Antibodies.pptx
Ashish Umale
How to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of SaleHow to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of Sale
Celine George
How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17
Celine George
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
Quiz Club of PSG College of Arts & Science
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. DabhadeSynthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Test Bank Pharmacology 3rd Edition Brenner Stevens
Test Bank Pharmacology 3rd Edition Brenner  StevensTest Bank Pharmacology 3rd Edition Brenner  Stevens
Test Bank Pharmacology 3rd Edition Brenner Stevens
evakimworwa38
Unit No. 4 - Immunopharmacologyslides.pptx
Unit No. 4 - Immunopharmacologyslides.pptxUnit No. 4 - Immunopharmacologyslides.pptx
Unit No. 4 - Immunopharmacologyslides.pptx
Ashish Umale
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
Prabhakar Singh Patel
Unit No 4- Chemotherapy of Malignancy.pptx
Unit No  4- Chemotherapy of Malignancy.pptxUnit No  4- Chemotherapy of Malignancy.pptx
Unit No 4- Chemotherapy of Malignancy.pptx
Ashish Umale
UNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptxUNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptx
HARIHARAN A
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. DabhadeCombinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. DabhadeIntroduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptxBIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
maniramkumar
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷sPurchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Celine George
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
Nguyen Thanh Tu Collection
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
A Biodiction : A Unit of Dr. Divya Sharma
Proteins, Bio similars & Antibodies.pptx
Proteins, Bio similars &  Antibodies.pptxProteins, Bio similars &  Antibodies.pptx
Proteins, Bio similars & Antibodies.pptx
Ashish Umale
How to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of SaleHow to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of Sale
Celine George
How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17
Celine George
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. DabhadeSynthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Test Bank Pharmacology 3rd Edition Brenner Stevens
Test Bank Pharmacology 3rd Edition Brenner  StevensTest Bank Pharmacology 3rd Edition Brenner  Stevens
Test Bank Pharmacology 3rd Edition Brenner Stevens
evakimworwa38

Regex Experession with Regex functions o

  • 2. String Matching The problem of finding a string that looks kind of like is common e.g. finding useful delimiters in a file, checking for valid user input, filtering email, Regular expressions are a common tool for this most languages support regular expressions in Java, they can be used to describe valid delimiters for Scanner (and other places)
  • 3. Matching When you give a regular expression (a regex for short) you can check a string to see if it matches that pattern e.g. Suppose that we have a regular expression to describe a comma then maybe some whitespace delimiters The string , would match that expression. So would , and , n But these wouldnt: , ,, word
  • 4. Note The finite state machines and regular languages from MACM 101 are closely related they describe the same sets of characters that can be matched with regular expressions (Regular expression implementations are sometimes extended to do more than the regular language definition)
  • 5. Basics When we specified a delimiter new Scanner().useDelimiter(,); the , is actually interpreted as a regular expression Most characters in a regex are used to indicate that character must be right here e.g. the regex abc matches only one string: abc literal translation: an a followed by a b followed by a c
  • 6. Repetition You can specify this character repeated some number of times in a regular expression e.g. match wot or woot or wooot A * says match zero or more of those A + says match one or more of those e.g. the regex wo+t will match the strings above literal translation: a w followed by one or more os followed by a t
  • 7. Example Read a text file, using comma and any number of spaces as the delimiter Scanner filein = new Scanner( new File(file.txt) ).useDelimiter(, *); while(filein.hasNext()) { System.out.printf((%s), filein.next()); } a comma followed by zero or more spaces
  • 8. Character Classes In our example, we need to be able to match any one of the whitespace characters In a regular expression, several characters can be enclosed in [] that will match any one of those characters e.g. regex a[123][45]will match these: a14 a15 a24 a25 a34 a35 An a; followed by a 1,2, or 3; followed by 4 or 5
  • 9. Example Read values, separated by comma, and one whitespace character: Scanner filein = new Scanner() .useDelimiter(,[ nt]); Whitespace technically refers to some other characters, but these are the most common: space, newline, tab java.lang.Character contains the real definition of whitespace
  • 10. Example We can combine this with repetition to get the right version a comma, followed by some (optional) whitespace Scanner filein = new Scanner() .useDelimiter(,[ nt]*); The regex matches a comma followed by zero or more spaces, newlines, or tabs. exactly what we are looking for
  • 11. More Character Classes A character range can be specified e.g. [0-9] will match any digit A character class can also be negated, to indicate any character except done by inserting a ^ at the start e.g.[^0-9] will match anything except a digit e.g.[^ nt] will match any non-whitespace
  • 12. Built-in Classes Several character classes are predefined, for common sets of characters . (period): any character d : any digit s : any space p{Lower} : any lower case letter These often vary from language to language. period is universal, s is common, p{Lower} is Java-specific (usually its [:lower:])
  • 13. Examples [A-Z] [a-z]* title case words (Title, I :not word or AB) p{Upper}p{Lower}* same as previous [0-9].* a digit, followed by anything (5q, 2345, 2) gr[ea]y grey or gray
  • 14. Other Regex Tricks Grouping: parens can group chunks together e.g. (ab)+ matches ab or abab or ababab e.g. ([abc] *)+ matches a or a b c, abc Optional parts: the question mark e.g. ab?c matches only abc and ac e.g. a(bc+)?d matches ad, abcd, abcccd, but not abd or accccd and many more options as well
  • 15. Other Uses Regular expressions can be used for much more than describing delimiters The Pattern class (in java.util.regex) contains Javas regular expression implementation it contains static functions that let you do simple regular expression manipulation and you can create Pattern objects that do more
  • 16. In a Scanner Besides separating tokens, a regex can be used to validate a token when its read by using the .next(regex) method if the next token matches regex, it is returned InputMismatchException is thrown if not This allows you to quickly make sure the input is in the right form. and ensures you dont continue with invalid (possibly dangerous) input
  • 17. Example Scanner userin = new Scanner(System.in); String word; System.out.println(Enter a word:); try{ word = userin.next([A-Za-z]+); System.out.printf( That word has %d letters.n, word.length() ); } catch(Exception e){ System.out.println(That wasnt a word); }
  • 18. Simple String Checking The matches function in Pattern takes a regex and a string to try to match returns a boolean: true if string matches e.g. in previous example could be done without an exception: word = userin.next(); if(matches([A-Za-z]+, word)) { // a word } else{ // give error message }
  • 19. Compiling a Regex When you match against a regex, the pattern must first be analyzed the library does some processing to turn it into some more-efficient internal format it compiles the regular expression It would be inefficient to do this many times with the same expression
  • 20. Compiling a Regex If a regex is going to be used many times, it can be compiled, creating a Pattern object it is only compiled when the object is created, but can be used to match many times The function Pattern.compile(regex) returns a new Pattern object
  • 21. Example Scanner userin = new Scanner(System.in); Pattern isWord = Pattern.compile([A-Za-z]+); Matcher m; String word; System.out.println(Enter some words:); do{ word = userin.next(); m = isWord.matcher(word); if(m.matches() ) { // a word } else { // not a word } } while(!word.equals(done) );
  • 22. Matchers The Matcher object that is created by patternObj.matcher(str) can do a lot more than just match the whole string give the part of the string that actually matched the expression find substrings that matched parts of the regex replace all matches with a new string Very useful in programs that do heavy string manipulation