如何设置一个等于“htp:// website htp:// website”的java字符串变量 [英] How to set a java string variable equal to "htp://website htp://website "
问题描述
所以我有一个很大的网站列表,我想把它们全部放在一个String变量中。我知道我无法单独访问所有链接并逃脱//,但是有超过几百个链接。有没有办法进行块逃逸,所以块之间的所有内容都被转义了?这是我想要在变量中保存的示例。
so I have a large list of websites and I want to put them all in a String variable. I know I can not individually go to all of the links and escape the //, but is there is over a few hundred links. Is there a way to do a "block escape", so everything in between the "block" is escaped? This is an example of what I want to save in the variable.
String links="http://website http://website http://website http://website http://website http://website"
也可以有人想到我可能遇到的任何其他问题这样做?
Also can anyone think of any other problems I might run into while doing this?
我做了htp而不是http,因为我不允许根据堆栈溢出发布超链接,因为我不在那个级别:p
I made it htp instead of http because I am not allowed to post "hyperlinks" according to stack overflow as I am not at that level :p
非常感谢
编辑:我正在编写一个程序因为我有大约50页的单词文档电子邮件和其他文字。我想过滤掉电子邮件。我编写程序来做这个非常简单,不是我只需要计算将页面存储在一个字符串变量中,程序将在其中运行。
I am making a program because I have about 50 pages of a word document that is filled with both emails and other text. I want to filter out just the emails. I wrote the program to do this which was very simple, not I just need to figure away to store the pages in a string variable in which the program will be run on.
推荐答案
你的问题写得不好。请改进它。在目前的格式中,它将被关闭为太模糊。
Your question is not well-written. Improve it, please. In its current format it will be closed as "too vague".
您想要过滤电子邮件或网站吗?您的示例是关于网站,您发送有关电子邮件的文本。虽然我不知道,但我决定尝试帮助你,我决定两者兼顾。
Do you want to filter e-mails or websites? Your example is about websites, you text about e-mails. As I don't know and I decided to try to help you anyway, I decided to do both.
以下是代码:
private static final Pattern EMAIL_REGEX =
Pattern.compile("[A-Za-z0-9](:?(:?[_\\.\\-]?[a-zA-Z0-9]+)*)@(:?[A-Za-z0-9]+)(:?(:?[\\.\\-]?[a-zA-Z0-9]+)*)\\.(:?[A-Za-z]{2,})");
private static final Pattern WEBSITE_REGEX =
Pattern.compile("http(:?s?)://[_#\\.\\-/\\?&=a-zA-Z0-9]*");
public static String readFileAsString(String fileName) throws IOException {
File f = new File(fileName);
byte[] b = new byte[(int) f.length()];
InputStream is = null;
try {
is = new FileInputStream(f);
is.read(b);
return new String(b, "UTF-8");
} finally {
if (is != null) is.close();
}
}
public static List<String> filterEmails(String everything) {
List<String> list = new ArrayList<String>(8192);
Matcher m = EMAIL_REGEX.matcher(everything);
while (m.find()) {
list.add(m.group());
}
return list;
}
public static List<String> filterWebsites(String everything) {
List<String> list = new ArrayList<String>(8192);
Matcher m = WEBSITE_REGEX.matcher(everything);
while (m.find()) {
list.add(m.group());
}
return list;
}
为了确保它有效,首先让我们测试filterEmails和filterWebsites方法:
To ensure that it works, first lets test the filterEmails and filterWebsites method:
public static void main(String[] args) {
System.out.println(filterEmails("Orange, pizza whatever else joe@somewhere.com a lot of text here. Blahblah blah with Luke Skywalker (luke@starwars.com) hfkjdsh fhdsjf jdhf Paulo <aaa.aaa@bgf-ret.com.br>"));
System.out.println(filterWebsites("Orange, pizza whatever else joe@somewhere.com a lot of text here. Blahblah blah with Luke Skywalker (http://luke.starwars.com/force) hfkjdsh fhdsjf jdhf Paulo <https://darth.vader/blackside?sith=true&midclorians> And the http://www.somewhere.com as x."));
}
输出:
[joe@somewhere.com, luke@starwars.com, aaa.aaa@bgf-ret.com.br]
[http://luke.starwars.com/force, https://darth.vader/blackside?sith=true&midclorians, http://www.somewhere.com]
测试readFileAsString方法:
To test the readFileAsString method:
public static void main(String[] args) {
System.out.println(readFileAsString("C:\\The_Path_To_Your_File\\SomeFile.txt"));
}
如果该文件存在,将打印其内容。
If that file exists, its content will be printed.
如果您不喜欢它返回 List< String>
而不是 String
项目除以空格,这很容易解决:
If you don't like the fact that it returns List<String>
instead of a String
with items divided by spaces, this is simple to solve:
public static String collapse(List<String> list) {
StringBuilder sb = new StringBuilder(50 * list.size());
for (String s : list) {
sb.append(" ").append(s);
}
sb.delete(0, 1);
return sb.toString();
}
坚持所有:
String fileName = ...;
String webSites = collapse(filterWebsites(readFileAsString(fileName)));
String emails = collapse(filterEmails(readFileAsString(fileName)));
这篇关于如何设置一个等于“htp:// website htp:// website”的java字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!