我正在使用java URL类从URL读取数据。问题是,我有一些字符串,我想摆脱使用正则表达式的引号和括号。请帮帮我。
我的输入
1 - alt="Shervin Champbell"
2 - alt=("Shervin Champbell")
结果应为
Shervin Champbell
我只是想摆脱这些引号和括号。我太努力但却徒劳无功。
我想摆脱alt,括号和引号
输出应该是: Shervin Champbell
这是我的代码
import java.io.*;
import java.util.regex.*;
public class URLReader {
public static void main(String[] args) throws Exception {
System.setProperty("http.proxyHost", "192.168.1.10");
System.setProperty("http.proxyPort", "8080");
URL url = new URL("http://www.ucp.edu.pk/information-technolo
/faculty-staff/faculty-staff.aspx");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
//found(inputLine);
names(inputLine);
in.close();
}
static void names(String name){
Pattern pattern = Pattern.compile("");
Matcher matcher = pattern.matcher(name);
if(matcher.find()){
String abc = name.substring(matcher.start(), matcher.end());
System.out.println(abc);
}
}
}
答案 0 :(得分:1)
http://rextester.com/replace/QYV56186不是非常强大,但适用于当前的示例。
答案 1 :(得分:0)
我正在考虑像这个正则表达式:
alt=[("]*(\w*[^)"]*)[)"]*
捕获的值是所需的输出
正则表达式字符串是:
"alt=[(\"]*(\\w*[^)\"]*)[)\"]*"
答案 2 :(得分:0)
你真的 用正则表达式吗?这似乎是艰难的方式。为什么不呢:
import java.util.*;
public class Strings {
public static void main(String[] args) {
String[] inputs = { "alt=\"Shervin Champbell\"",
"alt=(\"Shervin Champbell\")" };
for (String input : inputs) {
System.out.println(quotedStrings(input));
}
}
public static List<String> quotedStrings(String input) {
String[] parts = input.split("\"");
List<String> result = new ArrayList<>();
for (int i = 1; i < parts.length; i+=2) {
result.add(parts[i]);
}
return result;
}
}
输出(在括号中,因为它是列表):
[Shervin Champbell]
[Shervin Champbell]
如果需要,它还可以在输入字符串中处理多个带引号的字符串。
更好的是,只需使用commons-lang,已经通过StringUtils.substringBetween()找到一个字符串或StringUtils.substringsBetween()来查找多个字符串。