python,Linux,java学习疑问心得笔记3

python 1:正则表达式:匹配形式及输出内容

import re
print(re.search(r'ab','abcef'))

import re
print(re.search(r'd','abcef'))

 

import re
print(re.match(r'abc','abcef'))

import re
print(re.match(r'd','abcef'))

 

import re
print(re.fullmatch(r'abc','abcef'))
print(re.fullmatch(r'abc','abc'))

 

 2.匹配分裂

import re
print(re.split(r'\W+','ityard,ityard,ityard,array.'))
print(re.split(r'(\W+)','ityard,ityard,ityard'))
print(re.split(r'\W+','ityard,ityard,ityard.',1))
print(re.split('[a-f]+','1A2b3',flags=re.IGNORECASE))

import re
print(re.findall(r'ab','abefabdeab'))
print(re.findall(r'a','abefabdeab'))
it=re.finditer(r'\d+','12ab34cd56')
for match in it:
    print(match)
pattern=re.compile(r'#,*$')
str='ityard # 是我名字'
print(pattern.sub('',str))
print(pattern.subn('',str))

 3.匹配替换

import re
match=re.match(r'(?P<year>\w+) (?P<month>\w+)','2020 01')
print(match.expand(r'现在是\1年\2月'))
print(match.group(0))
print(match.group(1))
print(match.group(2))
print(match.groups())

import re
match=re.match(r'(?P<year>\w+) (?P<month>\w+)','2020 01')
print(match.groupdict())
print(match.start())
print(match.end())
print(match.span())

 JAVA:1.readLine()方法需要在程序入口加入throws IOException才能使用,java输入流可以System.in可以封装在BufferedReader中。及注意不等于字符串的写法

public static void main(String args[])throws IOException{
		BufferedReader br= new BufferedReader (new InputStreamReader(System.in));
		String str;
		System.out.println("Enter lines of text");
		System.out.println("Enter 'end' to quit");
		do {
			str=br.readLine();
			System.out.println(str);
		}while(!str.equals("end"));
	}

2.java发生找不到指定文件异常。可以把新建文档的路径写上。

DataInputStream d=new DataInputStream(new FileInputStream("test.txt"));
		DataOutputStream out=new DataOutputStream(new FileOutputStream("text1.txt"));
		String count;

异常:Exception in thread "main" java.io.FileNotFoundException: test.txt (系统找不到指定的文件。)

3.(问题)Java使用InputStream类和OutputStream类时,使用FileInputStream实列对象时输出在文档的是乱码。

public static void main(String args[]) {
		try {
			 byte bwrite[]= {11,21,3,40,5};
			 OutputStream os=new FileOutputStream("D:\\Program Files\\Java\\test.txt");
			 for(int i=0;i<bwrite.length;i++) {
				 os.write(bwrite);
			 }
			 os.close();
			 InputStream is=new FileInputStream("D:\\Program Files\\Java\\test.txt");
			 int size=is.available();
			 for(int i=0;i<size;i++) {
				 System.out.println((char)is.read()+"  ");
			 }
			 is.close();
		}catch(IOException e) {
			System.out.println("Exception");
		}
	}

4.append():实现字符串相连。但是内存是放在一起的,相当于String1+String2.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值