简体   繁体   English

java 中的正则表达式在 n 个字符后打印

[英]regular expression in java to print after n characters

I am writing java code to print something between two expressions.我正在编写 java 代码来打印两个表达式之间的内容。

My file from where I am reading is:我正在阅读的文件是:

 CAL|AB12345|hi, how are you }

my java code:我的 java 代码:

final Pattern pattern = Patter.compile("CAL|AB[0-9]{5}|(.+?)}", Pattern.DOTALL);
final Matcher matcher = pattern.matcher(filename);
while (matcher.find()) {
 String a = matcher.group(0);
  System.out.println(a);}

I want the output to be:我希望 output 是:

hi, how are you

ALso, how can i calculate the number of characters for example if i want to print after 9 characters after CAL, it will also work.另外,我如何计算字符数,例如,如果我想在 CAL 之后打印 9 个字符,它也可以工作。

you can modify your code like this:你可以像这样修改你的代码:

public static void main(String []args){

    final Pattern pattern = Pattern.compile("(CAL\\|AB[0-9]{5}\\|)([a-zA-Z0-9,><@#\\s]+)(})");
    final Matcher matcher = pattern.matcher("CAL|AB12345|hi, how are you }");

    String a;
    while (matcher.find()) {
         a = matcher.group(2);

        System.out.println(a);}
}
}

OUTPUT OUTPUT

 hi, how are you

Hint: for 9 characters part,you can start substring from that index count提示:对于 9 个字符部分,您可以从该索引计数开始 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ

Hope it helps !希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM