下面的代码用于获取某种形式的输入,其中包括重要字符串之间和重要字符串之前和之后的大量空格,到目前为止,我已经能够过滤掉空白。准备好字符串后,我想要做的就是处理它。
这是我可能获得的输入和我想要的有利输出的示例;
Input
+--------------+
EDIT example.mv Starter web-onyx-01.example.net.mv
注意域之前和之后的空格id如何,这个空格可以作为随机量结束。
Output
+--------------+
example.mv. in ns web-onyx-01.example.net.mv.
在输出中,重要的位是域(示例。)和关键字(in)以及关键字(ns)和主机(web-onyx-01.example.net.mv。)之间的空格。还要注意句点(“。 “)域名和主持人之后。另一部分是,如果它是一个(.mv)ccTLD,我们将不得不从字符串中删除该位,
我想要实现的是这种具有多行文本的转换,这意味着我想要处理一堆无序的混乱字符串列表并批处理它们以产生干净的输出。
代码绝不是任何好的设计,但这至少是我想出的。注意:我是一名仍在学习编程的初学者。我希望您的建议是改进代码以及解决手头的问题,即将输入转换为所需的输出。
P.S输出用于DNS中的区域文件,因此错误可能非常有问题。
到目前为止,我的代码正在接受来自textarea的文本,并将文本输出到另一个显示输出的textarea。只要数组长度为2和3,我的代码就可以工作,但是在任何更大的情况下都会失败。那么我如何能够动态处理输出到输出的大小与列表/数组可能在未来一样大?
String s = jTextArea1.getText();
Pattern p = Pattern.compile("ADD|EDIT|DELETE|Domain|Starter|Silver|Gold|ADSL Business|Pro|Lite|Standard|ADSL Multi|Pro Plus", Pattern.MULTILINE);
Matcher m = p.matcher(s);
s = m.replaceAll("");
String ms = s.replaceAll("(?m)(^\\s+|[\\t\\f ](?=[\\t\\f ])|[\\t\\f ]$|\\s+\\z)", "");
String[] last = ms.split(" ");
for (String test : last){
System.out.println(test);
}
System.out.println("The length of array is: " +last.length);
if (str.isContain(last[0], ".mv")) {
if (last.length == 2) {
for(int i = 0; i < last.length; i++) {
last[0] = last[0].replaceFirst(".mv", "");
System.out.println(last[0]);
last[i] += ".";
if (last[i] == null ? last[0] == null : last[i].equals(last[0])) {
last[i]+= " in ns ";
}
String str1 = String.join("", last);
jTextArea2.setText(str1);
System.out.println(str1);
}
}
else if (last.length == 3) {
for(int i = 0; i < last.length; i++) {
last[0] = last[0].replaceFirst(".mv", "");
System.out.println(last[0]);
last[i] += ".";
if (last[i] == null ? last[0] == null : last[i].equals(last[0])) {
last[i]+= " in ns ";
}
if (last[i] == null ? last[1] == null : last[i].equals(last[1])){
last[i] += "\n";
}
if (last[i] == null ? last[2] == null : last[i].equals(last[2])){
last[i] = last[0] + last[2];
}
String str1 = String.join("", last);
jTextArea2.setText(str1);
System.out.println(str1);
}
}
}
据我了解你的问题,你有以下几种形式的输入:
whitespace[command]whitespace[domain]whitespace[label]whitespace[target-domain]whitespace
您想将其转换为以下形式,以便很好地对齐多行:
[domain]. in ns [target-domain].
要做到这一点,我建议如下:
例:
String input = " EDIT domain1.mv Starter example.domain1.net.mv \n" +
" DELETE long-domain1.mv Silver long-example.long-domain1.net.mv \n" +
" ADD short-domain1.mv ADSL Business ex.sdomain1.net.mv \n";
//step 1: split the input into lines
String[] lines = input.split( "\n" );
//step 2: build a regular expression to check the line format and extract the domains - which are the (\S+) parts
Pattern pattern = Pattern.compile( "^\\s*(?:ADD|EDIT|DELETE)\\s+(\\S+)\\s+(?:Domain|Starter|Silver|Gold|ADSL Business|Pro|Lite|Standard|ADSL Multi|Pro Plus)\\s+(\\S+)\\s*$" );
List<String[]> lineList = new LinkedList<>();
int maxLengthDomain = 0;
int maxLengthTargetDomain = 0;
for( String line : lines )
{
//step 2: check the line
Matcher matcher = pattern.matcher( line );
if( matcher.matches() ) {
//step 2: extract the domains
String domain = matcher.group( 1 );
String targetDomain = matcher.group( 2 );
//step 3: get the maximum length of the domains
maxLengthDomain = Math.max( maxLengthDomain, domain.length() );
maxLengthTargetDomain = Math.max( maxLengthTargetDomain, targetDomain.length() );
lineList.add( new String[] { domain, targetDomain } );
}
}
//step 4: build the format string with variable lengths
String formatString = String.format( "%%-%ds in ns %%-%ds", maxLengthDomain + 5, maxLengthTargetDomain + 2 );
//step 5: build the output
for( String[] line : lineList ) {
System.out.println( String.format( formatString, line[0] + ".", line[1] + "." ) );
}
结果:
domain1.mv. in ns example.domain1.net.mv.
long-domain1.mv. in ns long-example.long-domain1.net.mv.
short-domain1.mv. in ns ex.sdomain1.net.mv.