开发者

Search strings in a multi-line string with regex and Java

I am quite disturbed by Java syntax for regex... Here is my (multi-line) string:

2054:(0020,0032) DS #36 [-249.28170196281\-249.28170196281\0] Image Position (
2098:(0020,0037) DS #12 [1\0\0\0\1\0] Image Orientation (Patient)
2118:(0020,0052) UI #52 [1.3.12.2.11...5.2.30.25....2.20....0.0.0]
2178:(0020,1040) LO #0 [] Position Reference Indicator
2222:(0028,0004) CS #12 [MONOCHROME2] Photometric Interpretation
2242:(0028,0010) US #2 [256] Rows
2252:(0028,0011) US #2 [256] Columns
2262:(0028,0030) DS #18 [1.953125\1.953125] Pixel Spacing
2288:(0028,0100) US #2 [16] Bits Allocated
2298:(0028,0101) US #2 [12] Bits Stored
2352:(0028,1055) LO #6 [Algo1] Window Center & Width Explanation

I need 1.953125 and 1.953125 from DS #18 [1.953125\1.953125] Pixel Spacing

I tried this:

Pattern p = Pattern.compile("DS #18 \\[([0-9\\.]*)\\\\([0-9\\.]*)\\] Pixel Spacing"); // os is my string above
System.out.println(m.matches()); //开发者_开发问答 false =(

but without success. Any idea? "Pattern.MULTILINE" do not change anything.

Thanks!


If you are trying to extract multiple occurrences from your input string, you can't use the matches() method, as it will try to match the whole input. So for multiple occurrences:

Pattern p = Pattern.compile("DS \\#18 \\[([0-9\\.]*)\\\\([0-9\\.]*)\\] Pixel Spacing",
                            Pattern.MULTILINE|Pattern.DOTALL); 
Matcher m = p.matcher( input );
while( m.find() ) {
    System.out.println("[ "+m.group( 1 )+", "+m.group( 2 )+" ]");
}

If you want a single occurrence, then you need to add .* at the beginning and end of your pattern instead:

Pattern p = Pattern.compile(".*DS \\#18 \\[([0-9\\.]*)\\\\([0-9\\.]*)\\] Pixel Spacing.*",
                            Pattern.MULTILINE|Pattern.DOTALL); 
System.out.println(m.matches());

Edson

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜