나는 라인을 일치시키고 지우는 regex
을 가지고있다. 모든 것이 그 아래에 있으며 (모든 것을 위에 두십시오).정규식에 대해 한 줄로 어떻게 일치합니까?
Two Part Ask: 1) Why won't this pattern match the given String text below? 2) How can I be sure to just match on a single line and not multiple lines? - The pattern has to be found on the same single line.
String text = "Keep this.\n\n\nPlease match junkhere this t-h-i-s is missing.\n"
+ "Everything should be deleted here but don't match this on this line" + "\n\n";
Pattern p = Pattern.compile("^(Please(\\s)(match)(\\s)(.*?)\\sthis\\s(.*))$", Pattern.DOTALL);
Matcher m = p.matcher(text);
if (m.find()) {
text = (m.replaceAll("")).replaceAll("[\n]+$", ""); // remove everything below at and below "Please match ... this"
System.out.println(text);
}
예상 출력 :
이 보관하십시오.
예상되는 출력은 무엇입니까? 어떤 결과물을 받고 있습니까? –
'^'와'$'가 resp와 일치하도록 원한다면. 각 라인의 시작/각 라인의 끝에서'Pattern.MULTILINE'을 사용해야합니다. 'Pattern.DOTALL'은 점과 일치하는 개행을 만듭니다. – fge