java.lang.Object
java.util.regex.Pattern
- All Implemented Interfaces:
- Serializable
- See Also:
- Top Examples, Source Code,
matches
, matches
, matcher
, compile
,
character sequences
public static final int CANON_EQ
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int CASE_INSENSITIVE
- See Also:
UNICODE_CASE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[241]Create a pattern that describes any word beginning with Java
By Anonymous on 2004/07/19 17:04:57 Rate
// Create a pattern that describes any word beginning with "Java", and
// which also "captures" whatever suffix follows the "Java" prefix. It
// uses the CASE_INSENSITIVE flag, so it matches any capitalization
Pattern p = Pattern.compile ( "\\bJava ( \\w* ) ", Pattern.CASE_INSENSITIVE ) ;
// This is a sentence we want to compare the pattern to.
String text = "Java is fun; JavaScript is funny.";
// Create a Matcher object to compare the pattern to the text
Matcher m = p.matcher ( text ) ;
// Now loop to find all matches of the pattern in the text
while ( m.find ( ) ) {
// For each match, print the text that matched the pattern, and its
// position within the string
System.out.println ( "Found '" + m.group ( 0 ) + "' at position " + m.start ( 0 ) ) ;
// Also, if there was a suffix, print the suffix.
if ( m.start ( 1 ) < m.end ( 1 ) ) System.out.println ( "Suffix is " + m.group ( 1 ) ) ;
}
[886]Create a pattern, and print the matches
By linuxsen on 2005/10/14 15:27:34 Rate
import java.util.prefs.*;
import java.math.*;
import java.util.regex.*;
public class RegTest5
{
public static void main ( String arg [ ] )
{
Pattern p = Pattern.compile ( "\\bJ ( \\w* ) ", Pattern.CASE_INSENSITIVE ) ; // ( \\w* ) //, Pattern.CASE_SENSITIVE
String text = "Java is fun; JavaScript is funny.; JFunny ; just";
Matcher m = p.matcher ( text ) ;
while ( m.find ( ) ) {
System.out.println ( "Found '" + m.group ( 0 ) + "' at position " + m.start ( 0 ) +"-" + m.end ( 0 ) ) ;
if ( m.start ( 0 ) < m.end ( 0 ) ) System.out.println ( "Suffix is " + m.group ( 1 ) ) ;
}
}
}
public static final int COMMENTS
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static Pattern compile(String regex)
- See Also:
- PatternSyntaxException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[525]Create a case insensitive pattern
By Anonymous on 2003/11/17 09:54:37 Rate
import java.util.regex.*;
Matcher matcher = null;
try {
Pattern p =
Pattern.compile ( regExp, Pattern.CASE_INSENSITIVE ) ;
matcher = p.matcher ( inputString ) ;
} catch ( PatternSyntaxException pse ) {
// Handle exception
} catch ( IllegalArgumentException iae ) {
// Handle exception
}
if ( matcher.matches ( ) == true )
System.out.println ( "Input string matches!" ) ;
public static Pattern compile(String regex,
int flags)
- See Also:
- PatternSyntaxException, IllegalArgumentException,
CANON_EQ
, UNICODE_CASE
, DOTALL
, MULTILINE
, CASE_INSENSITIVE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1921]Simple regular expression test utility
By Anonymous on 2007/08/15 11:14:54 Rate
import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexTestHarness {
public static void main ( String [ ] args ) {
Console console = System.console ( ) ;
if ( console == null ) {
System.err.println ( "No console." ) ;
System.exit ( 1 ) ;
}
while ( true ) {
Pattern pattern =
Pattern.compile ( console.readLine ( "%nEnter your regex: " ) ) ;
Matcher matcher =
pattern.matcher ( console.readLine ( "Enter input string to search: " ) ) ;
boolean found = false;
while ( matcher.find ( ) ) {
console.format ( "I found the text \"%s\" starting at " +
"index %d and ending at index %d.%n",
matcher.group ( ) , matcher.start ( ) , matcher.end ( ) ) ;
found = true;
}
if ( !found ) {
console.format ( "No match found.%n" ) ;
}
}
}
}
public static final int DOTALL
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int flags()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int LITERAL
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Matcher matcher(CharSequence input)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static boolean matches(String regex,
CharSequence input)
- See Also:
- PatternSyntaxException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int MULTILINE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String pattern()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static String quote(String s)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String[] split(CharSequence input)
- See Also:
split
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1230]Split a string
By Anonymous on 2004/12/27 19:49:12 Rate
// Create a pattern to match breaks
Pattern p = Pattern.compile ( " [ ,\\s ] +" ) ;
// Split input with the pattern
String [ ] result = p.split ( "one,two, three four , five" ) ;
for ( int i=0; i < result.length; i++ )
System.out.println ( result [ i ] ) ;
[1922]Split to get the text that falls on either side of any regular expression
By Anonymous on 2007/08/15 11:19:47 Rate
Since we're still using Pattern and Matcher objects, you can use split to get the text that falls on either side of any regular expression. Here's the example to split on digits instead:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class SplitDemo2 {
private static final String REGEX = "\\d";
private static final String INPUT = "one9two4three7four1five";
public static void main ( String [ ] args ) {
Pattern p = Pattern.compile ( REGEX ) ;
String [ ] items = p.split ( INPUT ) ;
for ( String s : items ) {
System.out.println ( s ) ;
}
}
}
OUTPUT:
one
two
three
four
five
public String[] split(CharSequence input,
int limit)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String toString()
- See Also:
- Object
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int UNICODE_CASE
- See Also:
CASE_INSENSITIVE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[219]Pattern test case
By shuu373 { at } oki { dot } com on 2003/04/11 03:58:06 Rate
import java.util.regex.Pattern;
import junit.framework.TestCase;
public class PatternTestCase extends TestCase {
public PatternTestCase ( String arg0 ) {
super ( arg0 ) ;
}
public void testPattern ( ) {
String s = "\\p { InHiragana } ";
String str = "?????????????????????????????????????????????????????????";
Pattern pattern = Pattern.compile ( s , Pattern.UNICODE_CASE ) ;
String [ ] result = pattern.split ( str ) ;
for ( int index = 0;index < result.length ;index ++ ) {
System.out.println ( result [ index ] ) ;
}
}
}
public static final int UNIX_LINES
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples