|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
||||
|
Regular Expressions
Regular Expressions in Java
Regular expressions provide a really fast and flexible method of matching strings and matching patterns in strings. The String class uses them to perform things like replacing text. These are commonly used by text editors and can make some tasks really easy. The package that you need to import is java.util.regex.*;. This contains a few classes that you will need. They are Pattern and Matcher. Regular Expressions Regular expressions can be a complicated but useful method in manipulating, and validating strings. First we will look at some simple regular expressions. Code:
[A-Z] Code:
return ch >= 'A' && ch <= 'Z'; The above will match: A, E, I, D, G It will not match: a, e, i, o, !, ?. Code:
[^A-Z] We can use brace brackets to indicate exactly how many times to match and expression. Example: Code:
[A-Z]{1,}
Matches: A, BC, DEF, GHIKL No match: , d, ef, gh, ijk In the No match list the first item is a blank entry. This signifies that the expression must be matched at least once. Code:
[a-z]{4}
Code:
[a-z]{4,5}
Now, what if you want to match something that is not in the a character class? Code:
[^AEIOUaeiou] There is A LOT more that you can do, but that is enough that we can look at how to use these methods in Java. Have a look at this: regular expressions tutorial for more on regular expressions. String Manipulation Before we look into pattern matching, we will use some regular expressions with the methods in the String class. String.matches method You have a string and you want to match it against a regular expression (to make sure it is valid). It might be a phone number, email, sql query or something else. Once you write the method it is as easy as writing Code:
if (s.matches("test")) {
// tests if s contains test. This is the same as if (s.equals("test")) but you will see why this is brillaint latter.
System.out.println("s = test");
} else {
System.out.println("s <> test");
}
Code:
[0-9]{3}-{1}[0-9]{3}-{1}[0-9]{4}
Consider this phone number: 111-111-1111 Is it valid? Let us your our regular expression to try it. Code:
s = "111-111-1111";
if (s.matches("[0-9]{3}-{1}[0-9]{3}-{1}[0-9]{4}")) {
System.out.println("Valid phone number.");
} else {
System.out.println("Invalid phone number.");
}
Quote:
111-111-111 Try this code: Code:
s = "111-111-111";
if (s.matches("[0-9]{3}-{1}[0-9]{3}-{1}[0-9]{4}")) {
System.out.println("Valid phone number.");
} else {
System.out.println("Invalid phone number.");
}
Quote:
Even better, is this concept applies in a lot of languages: Python, Java, Perl, PHP, VB. Almost any language you can name (except C++ ) has built-in support for regular expressions.String.replace method This method is used to replace parts of strings that match a pattern. We use the replace method and give it two parameters. One is a regular expression to replace, and the other is what to replace it with. Code:
String s = "Testing this is a something that is just a test."; Code:
s = s.replace("Test","game");
Output: gameing this is a something that is just a test. Notice that it is case-sensitive. Let us do something more fun, we want to replace all four letter words with ****. Why? We live in a planet, where it is a federal offense to use four letter words. A word is defined as at least one uppercase or lowercase letter.So our regular expression could be: Code:
\\b\\w{4}\\b
Now we simply just do: Code:
s = s.replaceAll("\\b\\w{4}\\b", "****");
System.out.println(s);
Quote:
![]() Matching Now, say you want to count the number of four letter words. This is where matching comes in handy. We are going to take a sentence, count the number of four letter words and display a message. The first thing we need to do is create a Pattern object. We can't use a constructor but we have to use the compile method and pass it a regular expression. We use p.matcher to set up for matching against the string. Now we just use a while loop to count the number of matches. Then we display the output. Code:
int nCount = 0;
String s = "Hi, we are from planet dude and want to bring you cake.";
Pattern p = Pattern.compile("\\b\\w{4}\\b");
Matcher m = p.matcher(s);
while (m.find()) {
nCount++;
}
if (nCount == 0) {
System.out.println("Good boy! +rep for you");
} else if (nCount == 1) {
System.out.println("We will excuse you for using the cursed word.");
} else {
System.out.println("OMFG! You use a lot of bad words. -rep, infracted, banned. *mad*");
}
Quote:
You have now learned the basics of how text editors work. Regex Library Earlier, I mentioned that I made a library of useful functions for validating text. Here is one method from it: Code:
public boolean isValidName(String sName) {
sName = sName.replaceAll("[^a-zA-Z]", "");
String sPattern = "^([&'\\s]*[A-Z]\\S+)+";
// Pattern p = Pattern.compile(sPattern,Pattern.CASE_INSENSITIVE);
// to match strings in a case insensitive way
Pattern p = Pattern.compile(sPattern);
Matcher m = p.matcher(sName);
return m.matches();
}
Others Surely, there is a C++ programmer reading this thinking WTF can I not do that? Look here: regular expressions. |
|
||||
|
Re: Regular Expressions
Regular expressions are amazingly handy and knowing how to use them is invaluable. +rep
__________________
Questions and Answers | Online News and Social Bookmarking | Code and Text Collaboration General Chat Forum |
|
||||
|
Re: Regular Expressions
No, I think Boost takes care of that minor "oversight"
Also, C++0x has Regular Expressions as a proposed addition to the Standard Library.Why didn't you mention the + and * qualifiers? [A-Z]+ just seems cleaner than [A-Z]{0,}
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
||||
|
Re: Regular Expressions
C++ was standardized before Regular Expressions were a "cool", "must-have" language feature.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
||||
|
Re: Regular Expressions
__________________
![]() Google - I'm Feeling Lucky | Youtube Classes! My Portfolio | Email Me | QuestionBin |
|
||||
|
Re: Regular Expressions
Btw, wouldnt let me rep you =/ I needa spread the love around a bit!
__________________
![]() Google - I'm Feeling Lucky | Youtube Classes! My Portfolio | Email Me | QuestionBin |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| regular expressions | Lop | C and C++ | 5 | 09-16-2008 11:38 AM |
| Regular Expressions | John | PHP Tutorials | 27 | 09-11-2008 03:25 PM |
| Async Computation Expressions - Resource and Exception Management | Kernel | News | 0 | 08-16-2008 05:50 PM |
| Regular expressions | Nightracer | General Programming | 6 | 07-24-2006 10:57 PM |
All times are GMT -5. The time now is 11:01 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc