java之字符串正则(下)
Posted 今进
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java之字符串正则(下)相关的知识,希望对你有一定的参考价值。
要求修改一个字符串
user<div>user</div><p>user</p>user<div>myuser</div>
String s = "user<div>javauserok</div><p>user</p>user<div>myuser</div>--<div>aa</div><div></div>";
//System.out.println(s);
//System.out.println(s.replace("user", "java"));
/*
Pattern p = Pattern.compile("[a-zA-z]{4}");
Matcher m = p.matcher(s);
while(m.find()) {
System.out.println(m.group());
}
*/
//取出每个div中的内容,并显示
//Pattern p = Pattern.compile("<div>.*?</div>"); //禁用贪婪 如果去掉?号则是贪婪
Pattern p = Pattern.compile("(<div>.*?)(user)(.*?</div>)"); //禁用贪婪 如果去掉?号则是贪婪
Matcher m = p.matcher(s);
System.out.println(s);
System.out.println(m.replaceAll("$1****$3"));
while(m.find()) {
System.out.println(m.group());
}
5、要求注册的会员姓名,要么全中文,要么全英文,不能出现特殊符号及中英混合。
[a-zA-Z]+|[u4e00-u9fa5]+
String n = "李";
if(n.matches("[a-zA-Z ]+|[\u4e00-\u9fa5]{2,4}")) {
System.out.println("正确");
}else {
System.out.println("姓名非法");
}
^开头
$ 结束
d [0-9]
D [^0-9]
w [a-zA-Z0-9_]
W
[u4e00-u9fa5]
替换
禁用贪婪模式
贪婪模式
词典
String w = "chinese";
w = JOptionPane.showInputDialog("请输入要翻译的内容");
String url = String.format("http://www.youdao.com/w/eng/%s/#keyform=dict2.index", w);
if(w.matches("[u4e00-u9fa5]+")) {
url = String.format("http://www.youdao.com/w/%s/#keyform=dict2.index", w);
}
Document doc = Jsoup.connect(url).get();
Elements es = doc.select("div[class=trans-container]");
StringBuilder result = new StringBuilder();
for(Element e : es) {
result.append(e.text());
break;
}
JOptionPane.showMessageDialog(null, result.toString());
二维码 maven 项目 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.beiyou</groupId>
<artifactId>my</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
</dependencies>
</project>
package org;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class QrTest {
public static void main(String[] args) throws WriterException, FileNotFoundException, IOException {
BitMatrix qr = new MultiFormatWriter().encode("中文效果", BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToStream(qr, "png", new FileOutputStream("d:/qr.png"));
}
}
package org;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class Demo {
public static void main(String[] args) throws IOException, WriterException {
String w = "chinese";
w = JOptionPane.showInputDialog("请输入要翻译的内容");
String url = String.format("http://www.youdao.com/w/eng/%s/#keyform=dict2.index", w);
if(w.matches("[u4e00-u9fa5]+")) {
url = String.format("http://www.youdao.com/w/%s/#keyform=dict2.index", w);
}
Document doc = Jsoup.connect(url).get();
Elements es = doc.select("div[class=trans-container]");
StringBuilder result = new StringBuilder();
for(Element e : es) {
result.append(e.text());
break;
}
JOptionPane.showMessageDialog(null, result.toString());
Map h = new HashMap();
h.put(EncodeHintType.CHARACTER_SET,"utf-8");
BitMatrix dt = new MultiFormatWriter().encode(w+" : "+result.toString(), BarcodeFormat.QR_CODE, 600, 600,h);
MatrixToImageWriter.writeToStream(dt,"png",new FileOutputStream("d:/"+w+".png"));
}
}
以上是关于java之字符串正则(下)的主要内容,如果未能解决你的问题,请参考以下文章