2

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!

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

FYI, the MULTILINE and DOTALL modifiers are unnecessary. MULTILINE alters the meaning of the anchors (^ and $), and DOTALL alters the meaning of the dot (.). Since you aren't using the dot as a metacharacter, and you're not using the anchors at all, neither of those modifiers is relevant.
Yes, only the second example requires the DOTALL. I just usually use both by default when doing multiline matches so that I don't need to worry about adding/removing the options when I change my pattern, but you are right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.