简体   繁体   English

Java中的特殊字符后的正则表达式

[英]Regex after a special character in Java

I am using regex in java to get a specific output from a list of rooms at my University. 我在Java中使用正则表达式从大学的房间列表中获取特定输出。

A outtake from the list looks like this: 清单中的支出如下所示:

  • (A55:G260) Laboratorium 260 (A55:G260)实验室260
  • (A55:G292) Grupperom 292 (A55:G292)Grupperom 292
  • (A55:G316) Grupperom 316 (A55:G316)Grupperom 316
  • (A55:G366) Grupperom 366 (A55:G366)Grupperom 366
  • (HDS:FLØYEN) Fløyen (appendix) (HDS:FLØYEN)Fløyen(附录)
  • (ODO:PC-STUE) Pulpakammeret (PC-stue) (ODO:PC-STUE)Pulpakammeret(PC-stue)
  • (SALEM:KONF) Konferanserom (SALEM:KONF)Konferanserom

I want to get the value that comes between the colon and the parenthesis. 我想获得介于冒号和括号之间的值。

The regex I am using at the moment is: 我目前使用的正则表达式是:

pattern = Pattern.compile("[:]([A-Za-z0-9ÆØÅæøå-]+)");
matcher = pattern.matcher(room.text());

I've included ÆØÅ, because some of the rooms have Norwegian letters in them. 我包括了ÆØÅ,因为其中一些房间里有挪威字母。

Unfortunately the regex includes the building code also (eg "A55") in the output... Comes out like this: 不幸的是,正则表达式还在输出中包括了建筑代码(例如“ A55”)...像这样出来:

A55
A55
A55
:G260
:G292
:G316

Any ideas on how to solve this? 关于如何解决这个问题的任何想法?

The problem is not your regular expression. 问题不在于您的正则表达式。 You need to reference group( 1 ) for the match result. 您需要引用group( 1 )作为匹配结果。

while (matcher.find()) {
  System.out.println(matcher.group(1));
}

However, you may consider using a negated character class instead. 但是,您可以考虑使用否定字符类。

pattern = Pattern.compile(":([^)]+)");

You can try a regex like this : 您可以尝试这样的正则表达式:

public static void main(String[] args) {
    String s = "(HDS:FLØYEN) Fløyen (appendix)";
    // select everything after ":" upto the first ")" and replace the entire regex with the selcted data
    System.out.println(s.replaceAll(".*?:(.*?)\\).*", "$1"));
    String s1 = "ODO:PC-STUE) Pulpakammeret (PC-stue)";
    System.out.println(s1.replaceAll(".*?:(.*?)\\).*", "$1"));
}

O/P : O / P:

FLØYEN
PC-STUE

Can try with String Opreations as follows, 可以尝试使用String Opreations,如下所示:

String val = "(HDS:FLØYEN) Fløyen (appendix)";

if(val.contains(":")){

    String valSub = val.split("\\s")[0];
    System.out.println(valSub);

    valSub = valSub.substring(1, valSub.length()-1);

    String valA = valSub.split(":")[0];
    String valB = valSub.split(":")[1];

    System.out.println(valA);
    System.out.println(valB);

}

Output : 输出:

(HDS:FLØYEN)
HDS
FLØYEN 

import java.util.regex.Matcher; 导入java.util.regex.Matcher; import java.util.regex.Pattern; 导入java.util.regex.Pattern;

class test { public static void main( String args[] ){ 类测试{public static void main(String args []){

  // String to be scanned to find the pattern.
  String line = "(HDS:FLØYEN) Fløyen (appendix)";
  String pattern = ":([^)]+)";

  // Create a Pattern object
  Pattern r = Pattern.compile(pattern);

  // Now create matcher object.
  Matcher m = r.matcher(line);
  while (m.find()) {
    System.out.println(m.group(1));
}

} } }}

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

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