5、
package ch13;
import java.util.*;
public class Explain {
int a;
long b;
float c;
double d;
String e;
static String f="3 400 2.1 2.2 hi";
Explain(String s){
Scanner std=new Scanner(s);
a=std.nextInt();
b=std.nextLong();
c=std.nextFloat();
d=std.nextDouble();
e=std.next();
}
public String toString() {
return a+""+b+""+c+""+d+""+e;
}
public static void main(String[] args){
Explain ex=new Explain(f);
System.out.println(ex);
}
}
6、
package ch12;
import java.util.*;
public class Opq {
int c1=3;
long c2=40;
float c3=14f;
double c4=15.2f;
public String toString() {
return String.format("c1 is %d,c2 is %d,c3 is %f,c4 is %f",c1,c2,c3,c4);
}
public static void main(String[] args) {
Opq o=new Opq();
System.out.println(o);
}
}
7、
package ch12;
import java.util.*;
public class Opq {
static boolean tp(String sen){
return sen.matches("[A-Z]+.*[\\.]");
}
public static void main(String[] args) {
System.out.println(tp("Adff."));
System.out.println(tp("Rdff."));
System.out.println(tp("sffh"));
}
}
8、
package ch12;
import java.util.*;
public class Opq {
static String knights="Then,when you have found the shrubbery,"
+ "you must cut down the mightiest tree in the forest..."+
"with... a herring!";
public static void split(String regex) {
System.out.println(Arrays.toString(knights.split(regex)));
}
public static void main(String[] args) {
split("(the|you)");
}
}
9、
package ch12;
import java.util.*;
public class Opq {
static String knights="Then,when you have found the shrubbery,"
+ "you must cut down the mightiest tree in the forest..."+
"with... a herring!";
public static void split(String regex) {
System.out.println(Arrays.toString(knights.split(regex)));
}
public static void main(String[] args) {
System.out.println(knights.replaceAll("(A|E|I|O|U|a|e|i|o|u)", "_"));
}
}
13、
package ch12;
import java.util.regex.*;
public class StartEnd {
public static String POEM=
"Twas brillig, and the slithy toves\n"+
"Did gyre and gimble in the wabe.\n"+
"All mimsy were the borogoves,\n"+
"And the mome raths outgrade.\n\n"+
"Beware the Jabberwock, my son,\n"+
"The jaws that bite, the claws that catch.\n"+
"Beware the Jubjub bird, and shun\n"+
"The frumious Bandersnatch.";
private static class Display{
private boolean regexPrinted=false;
private String regex;
Display(String regex){this.regex=regex;}
void display(String message) {
if(!regexPrinted) {
System.out.println(regex);
regexPrinted=true;
}
System.out.println(message);
}
}
static void examine(String s,String regex) {
Display d=new Display(regex);
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(s);
while(m.find())
d.display("find()'"+m.group()+"'start="+m.start()+"end="+m.end());
if(m.lookingAt())
d.display("lookingAt() start="+m.start()+"end="+m.end());
if(m.matches())
d.display("matches() start="+m.start()+"end="+m.end());
}
public static void main(String[] args) {
for(String in:POEM.split("\n")) {
System.out.println("input:"+in);
for(String regex:new String[] {"\\w*ere\\w*","\\w*e","The.*"})
examine(in,regex);
}
}
}
14、
package ch12;
import java.util.regex.*;
import java.util.*;
public class SplitDemo {
public static void main(String[] args) {
String input="This!!unusual use!!of exclamation!!points";
//System.out.println(Arrays.toString(Pattern.compile("!!").split(input)));
//System.out.println(Arrays.toString(Pattern.compile("!!").split(input,3)));
System.out.println(Arrays.toString(input.split("!!")));
System.out.println(Arrays.toString(input.split("!!",3)));
}
}
15、
package ch12;
import java.util.regex.*;
import net.mindview.util.*;
public class JGrep {
public static void main(String[] args) throws Exception{
if(args.length<2) {
System.out.println("Usage:java JGrep file regex");
System.exit(0);
}
//String ha=args[1];
Pattern p=Pattern.compile(args[1],Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
int index=0;
Matcher m=p.matcher("");
String line2="";
for(String line:new TextFile(args[0])) {
line2=line2+"\n"+line;
}
//System.out.println(line);
//String line=String(new TextFile(args[0]));
m.reset(line2);
while(m.find())
System.out.println(index++ +":"+m.group()+":"+m.start());
}
}
17、
package ch13;
import java.util.regex.*;
import net.mindview.util.*;
/*show it
for everybody
*/
//i want to look it
/*
*send to show
*/
//again
public class Explain {
public static void main(String[] args) throws Exception{
String s=TextFile.read("src/ch13/Explain.java");
Matcher m1=Pattern.compile("/\\*(.*?)\\*/",Pattern.DOTALL).matcher(s);
while(m1.find())
System.out.println(m1.group(1));
Matcher m2=Pattern.compile("//(.*)").matcher(s);
while(m2.find())
System.out.println(m2.group(1));
}
}
19、
package ch13;
import java.util.regex.*;
import net.mindview.util.*;
import java.io.*;
/*"what" "and" "haow"
*/
public class Explain {
class Ex{}
public static void main(String[] args) throws Exception{
if(args.length < 1) {
System.out.println("Usage: fileName");
System.exit(0);
}
Pattern p = Pattern.compile("class (\\w+)");
// Iterate through the lines of the input file:
int index = 0;
Matcher m = p.matcher(""); // creates emtpy Matcher object
for(String line : new TextFile(args[0])) {
m.reset(line);
while(m.find())
System.out.println(index++ + ": " + m.group(1));
}
}
}
20、
package ch13;
import java.util.*;
public class Explain {
int a;
long b;
float c;
double d;
String e;
static String f="3 400 2.1 2.2 hi";
Explain(String s){
Scanner std=new Scanner(s);
a=std.nextInt();
b=std.nextLong();
c=std.nextFloat();
d=std.nextDouble();
e=std.next();
}
public String toString() {
return a+""+b+""+c+""+d+""+e;
}
public static void main(String[] args){
Explain ex=new Explain(f);
System.out.println(ex);
}
}