- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此类打开一个文件,在 //
处分隔并返回一个字符串。然后,我使用匹配器的模式集来搜索字符串并返回数据片段。稍后,这将用于将数据重新格式化为许多文件和特定命令。目前,此过程已适用于多个模式匹配,但当我传递 EditorList
和 AuthorList
时,当数据明确存在时,它会返回 null。当程序尝试使用空字符串时会崩溃,并且出现空指针异常。这是我第一次使用 Pattern 和 Matcher,我在这里忽略了什么明显的事情?
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchMethod {
public static String cancerCat=null;
public static String paperType="book";
public static String Paper=null;
public static String Title=null;
public static String Abstr=null;
public static String Publi=null;
public static String Editi=null;
public static String Pagen=null;
public static String Bookt=null;
public static String Years=null;
public static String Editl=null;
public static String Edito=null;
public static String Authl=null;
public static String Autho=null;
public static String Foren=null;
public static String Initi=null;
public static String Lastn=null;
public static Scanner scanner;
public static File file;
static Pattern PaperBegin = Pattern.compile("<PaperBegin>(.+?)</PaperBegin>");
static Pattern PaperTitle = Pattern.compile("<PaperTitle>(.+?)</PaperTitle>");
static Pattern Abstract = Pattern.compile("<Abstract>(.+?)</Abstract>");
static Pattern BookTitle = Pattern.compile("<BookTitle>(.+?)</BookTitle>");
static Pattern Publisher = Pattern.compile("<Publisher>(.+?)</Publisher>");
static Pattern Edition = Pattern.compile("<Edition>(.+?)</Edition>");
static Pattern Page = Pattern.compile("<Page>(.+?)</Page>");
static Pattern EditorList = Pattern.compile("<EditorList>(.+?)</EditorList>");
static Pattern Editor = Pattern.compile("<Editor>(.+?)</Editor>");
static Pattern Year = Pattern.compile("<Year>(.+?)</Year>");
static Pattern AuthorList = Pattern.compile("<AuthorList>(.+?)</AuthorList>");
static Pattern Author = Pattern.compile("<Author>(.+?)</Author>");
static Pattern ForeName = Pattern.compile("<ForeName>(.+?)</ForeName>");
static Pattern Initials = Pattern.compile("<Initials>(.+?)</Initials>");
static Pattern LastName = Pattern.compile("<LastName>(.+?)</LastName>");
public static String find (String text, Pattern pattern)
{
String found=null;
Matcher match = pattern.matcher(text);
if (match.find()) {found = match.group(1);}
System.out.println((pattern.toString()) + " found: "+found);
return found;
}
@SuppressWarnings("resource")
static void readBook (String book) throws FileNotFoundException
{
file = new File (book);
scanner = new Scanner(file).useDelimiter("\\//");
while (scanner.hasNext())
{
Paper=scanner.next();
Title = find (Paper, PaperTitle);
Abstr = find (Paper, Abstract);
Publi = find (Paper, Publisher);
Editi = find (Paper, Edition);
Pagen = find (Paper, Page);
Bookt = find (Paper, BookTitle);
Years = find (Paper, Year);
Editl = find (Paper, EditorList);
Authl = find (Paper, AuthorList);
Matcher mEdito = Editor.matcher(Editl);
Edito = mEdito.group(1);
while (mEdito.find()) // while loop to find all editors
{
System.out.println("Searching editors");
Foren = find (Edito, ForeName);
Initi = find (Edito, Initials);
Lastn = find (Edito, LastName);
System.out.println ("EDITORS: " + Bookt + "\t" + Foren + "\t" + Initi + "\t" + Lastn);
}
Matcher mAutho = Author.matcher(Authl);
while (mAutho.find()) // while loop to find all editors
{
System.out.println("Searching authors");
Autho = mAutho.group(1);
Foren = find (Autho, ForeName);
Initi = find (Autho, Initials);
Lastn = find (Autho, LastName);
System.out.println ("AUTHORS: " + Bookt + "\t" + Foren + "\t" + Initi + "\t" + Lastn);
}
}
}
public static void main(String[] args) throws IOException
{
readBook ("CC_book.txt"); //opens text file to be mined
//Start reading Colon Cancer Book Information
//Start reading Endocrine Cancer Book Information
//Start reading Lung Cancer Book Information
//Start reading Other Cancer Book Information
//Start reading Pancreatic Cancer Book Information
scanner.close();
}
}
以下是文件中的示例数据:
<PaperTitle>True incidence of all complications following immediate and delayed breast reconstruction.</PaperTitle>
<Abstract>BACKGROUND: Improved self-image and psychological well-being after breast reconstruction are well documented. To determine methods that optimized results with minimal morbidity, the authors examined their results and complications based on reconstruction method and timing. METHODS: The authors reviewed all breast reconstructions after mastectomy for breast cancer performed under the supervision of a single surgeon over a 6-year period at a tertiary referral center. Reconstruction method and timing, patient characteristics, and complication rates were reviewed. RESULTS: Reconstruction was performed on 240 consecutive women (94 bilateral and 146 unilateral; 334 total reconstructions). Reconstruction timing was evenly split between immediate (n = 167) and delayed (n = 167). Autologous tissue (n = 192) was more common than tissue expander/implant reconstruction (n = 142), and the free deep inferior epigastric perforator was the most common free flap (n = 124). The authors found no difference in the complication incidence with autologous reconstruction, whether performed immediately or delayed. However, there was a significantly higher complication rate following immediate placement of a tissue expander when compared with delayed reconstruction (p = 0.008). Capsular contracture was a significantly more common late complication following immediate (40.4 percent) versus delayed (17.0 percent) reconstruction (p < 0.001; odds ratio, 5.2; 95 percent confidence interval, 2.3 to 11.6). CONCLUSIONS: Autologous reconstruction can be performed immediately or delayed, with optimal aesthetic outcome and low flap loss risk. However, the overall complication and capsular contracture incidence following immediate tissue expander/implant reconstruction was much higher than when performed delayed. Thus, tissue expander placement at the time of mastectomy may not necessarily save the patient an extra operation and may compromise the final aesthetic outcome.</Abstract>
<BookTitle>Book1</BookTitle>
<Publisher>Publisher01, Boston</Publisher>
<Edition>1st</Edition>
<EditorList>
<Editor>
<LastName>Lewis</LastName>
<ForeName>Philip M</ForeName>
<Initials>PM</Initials>
</Editor>
<Editor>
<LastName>Kiffer</LastName>
<ForeName>Michael</ForeName>
<Initials>M</Initials>
</Editor>
</EditorList>
<Page>19-28</Page>
<Year>2008</Year>
<AuthorList>
<Author ValidYN="Y">
<LastName>Sullivan</LastName>
<ForeName>Stephen R</ForeName>
<Initials>SR</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Fletcher</LastName>
<ForeName>Derek R D</ForeName>
<Initials>DR</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Isom</LastName>
<ForeName>Casey D</ForeName>
<Initials>CD</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Isik</LastName>
<ForeName>F Frank</ForeName>
<Initials>FF</Initials>
</Author>
</AuthorList>
//
<PaperTitle>Polygenes, risk prediction, and targeted prevention of breast cancer.</PaperTitle>
<Abstract>BACKGROUND: New developments in the search for susceptibility alleles in complex disorders provide support for the possibility of a polygenic approach to the prevention and treatment of common diseases. METHODS: We examined the implications, both for individualized disease prevention and for public health policy, of findings concerning the risk of breast cancer that are based on common genetic variation. RESULTS: Our analysis suggests that the risk profile generated by the known, common, moderate-risk alleles does not provide sufficient discrimination to warrant individualized prevention. However, useful risk stratification may be possible in the context of programs for disease prevention in the general population. CONCLUSIONS: The clinical use of single, common, low-penetrance genes is limited, but a few susceptibility alleles may distinguish women who are at high risk for breast cancer from those who are at low risk, particularly in the context of population screening.</Abstract>
<BookTitle>Book2</BookTitle>
<Publisher>Publisher02, New York</Publisher>
<Edition>3rd</Edition>
<EditorList>
<Editor>
<LastName>Bernstein</LastName>
<ForeName>Arthur</ForeName>
<Initials>A</Initials>
</Editor>
</EditorList>
<Page>2796-803</Page>
<Year>2008</Year>
<AuthorList>
<Author ValidYN="Y">
<LastName>Pharoah</LastName>
<ForeName>Paul D P</ForeName>
<Initials>PD</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Antoniou</LastName>
<ForeName>Antonis C</ForeName>
<Initials>AC</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Easton</LastName>
<ForeName>Douglas F</ForeName>
<Initials>DF</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Ponder</LastName>
<ForeName>Bruce A J</ForeName>
<Initials>BA</Initials>
</Author>
</AuthorList>
//
<PaperTitle>Invasive breast cancer: predicting disease recurrence by using high-spatial-resolution signal enhancement ratio imaging.</PaperTitle>
<Abstract>PURPOSE: To retrospectively evaluate high-spatial-resolution signal enhancement ratio (SER) imaging for the prediction of disease recurrence in patients with breast cancer who underwent preoperative magnetic resonance (MR) imaging. MATERIALS AND METHODS: This retrospective study was approved by the institutional review board and was HIPAA compliant; informed consent was waived. From 1995 to 2002, gadolinium-enhanced MR imaging data were acquired with a three time point high-resolution method in women undergoing neoadjuvant therapy for invasive breast cancers. Forty-eight women (mean age, 49.1 years; range, 29.7-72.4 years) were divided into recurrence-free or recurrence groups. Volume measurements were tabulated for SER values between set ranges; cutoff criteria were defined to predict disease recurrence after surgery. Wilcoxon rank sum tests and the multivariate Cox proportional hazards regression model were used for evaluation. RESULTS: Breast tumor volume calculated from the number of voxels with SER values above a threshold corresponding to the upper limit of mean redistribution rate constant in benign tumors (0.88 minutes(-1)) and the volume of cancerous breast tissue infiltrating into the parenchyma were important predictors of disease recurrence. Seventy-five percent of patients with recurrence and 100% of deceased patients were identified as being at high risk for recurrence. Thirty percent of patients with recurrence and 67% of deceased patients were identified as having high risk before chemotherapy. No patients in the recurrence-free group were misidentified as likely to have recurrence. All three prechemotherapy parameters (total tumor volume, tumor volumes with high and low SER) and the postchemotherapy tumor volume with high SER were significantly different between the two groups. The multivariate Cox proportional hazards regression showed that, of the three prechemotherapy covariates, only the low SER and high SER tumor volumes (P = .017 and .049, respectively) were significant and independent predictors of tumor recurrence. Tumor volume with high SER was the only significant postchemotherapy covariate predictor (P = .038). CONCLUSION: High-spatial-resolution SER imaging may improve prediction for patients at high risk for disease recurrence and death.</Abstract>
<BookTitle>Book3</BookTitle>
<Publisher>Publisher03, London</Publisher>
<Edition>3rd</Edition>
<EditorList>
<Editor>
<LastName>Anderson</LastName>
<ForeName>John T</ForeName>
<Initials>JT</Initials>
</Editor>
<Editor>
<LastName>Hoffman</LastName>
<ForeName>John A</ForeName>
<Initials>JA</Initials>
</Editor>
<Editor>
<LastName>Smithson</LastName>
<ForeName>Joshua H</ForeName>
<Initials>JH</Initials>
</Editor>
</EditorList>
<Page>79-87</Page>
<Year>2008</Year>
<AuthorList>
<Author ValidYN="Y">
<LastName>Li</LastName>
<ForeName>Ka-Loh</ForeName>
<Initials>KL</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Partridge</LastName>
<ForeName>Savannah C</ForeName>
<Initials>SC</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Joe</LastName>
<ForeName>Bonnie N</ForeName>
<Initials>BN</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Gibbs</LastName>
<ForeName>Jessica E</ForeName>
<Initials>JE</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Lu</LastName>
<ForeName>Ying</ForeName>
<Initials>Y</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Esserman</LastName>
<ForeName>Laura J</ForeName>
<Initials>LJ</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Hylton</LastName>
<ForeName>Nola M</ForeName>
<Initials>NM</Initials>
</Author>
</AuthorList>
//
最佳答案
我知道java必须有很多用于解析xml的工具,并且使用正则表达式解析xml,除此之外,可能会变得非常困惑。
<小时/>我不是 Java 程序员,但您可能会遇到 .默认情况下不匹配换行符。您可以使用开关 (?s)
为所有正则表达式添加前缀,该开关实际上仅适用于 Editor、Author、EditorList 和 AuthorList。
例如,您编写的正则表达式看起来像这样。
static Pattern Author = Pattern.compile("(?s)<Author>(.+?)</Author>");
来源: Regular expression does not match newline obtained from Formatter object
<小时/>关于您评论的内容
... This is another problem, since if it is empty it should just return another null string. ...
您的正则表达式不会执行此操作的原因是您正在使用 (.+?)。如果您将每次出现的情况(如果适用)更改为 (.*?)
,则允许空字符串。 .+ 需要在开始标签和结束标签之间有一个字符(任何字符)。 .* 不需要角色,但可以获取任何礼物。还有?使匹配变得非贪婪,因此只要满足条件就会捕获。
Consider the string: I like cats, I wonder if you like cats
"I (.*) cats" matches the whole string.
"I (.*?) cats" matches "I like cats", and if the global flag is on, seperately matches "I wonder if you like cats"
<小时/>
您确定是 AuthorList
/EditorList
导致了崩溃吗?
您的 Author
正则表达式根本不考虑属性 ValidYN
,但此示例数据中的每个实例都包含它,因此您应该匹配它。
对于作者
正则表达式尝试
<Author(?: [\w\-]*="[^"]*")*>(.+?)</Author>
这是一个简单的模式,用于查找属性中包含字母、数字、_ 或连字符的属性以及本身不能包含引号的带引号的属性。
或者更简单,如果 ValidYN 是您将遇到的唯一属性:
<Author ValidYN="(?:Y|N)">(.+?)</Author>
但是,如果出现该问题,第一个正则表达式可能会方便地处理可能具有属性的其他标签。
关于Java 模式匹配器在与之前的九个相似模式/匹配完美配合后返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27197634/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!