Reputation: 2431
I am attempting to use replaceAll to delete parenthesis and commas in a numeric line of code as shown below:
00020001 OB #2 [00\01] File Meta Information Version
00020002 UI #26 [1.2.840.10008.5.1.4.1.1.4] Media Storage SOP Class UID
00020003 UI #62 [1.3.46.670589.11.0.0.11.4.2.0.8743.5.5396.2006120114483570701] Media Storage SOP
00020010 UI #20 [1.2.840.10008.1.2.1] Transfer Syntax UID
Using this line of code:
String replaced = dcmObj.toString().replaceAll("[(),*]", "");
I want to take it one step further and delete everything past the numeric code so for example:
00020010 UI #20 [1.2.840.10008.1.2.1] Transfer Syntax UID
Will be turned to just:
00020010
Can someone show me how I would do that with the line of code I am using?
Upvotes: 1
Views: 55
Reputation: 124275
You can try storing number placed ad start of line in group and replace entire line with match from this group.
yourTest = yourText.replaceAll("(?m)^(\\d+).*", "$1");
(?m)
represents MULTILINE flag which lets ^
match start of line, not only start of entire string.
DEMO:
String s =
"00020001 OB #2 [00\\01] File Meta Information Version\r\n" +
"00020002 UI #26 [1.2.840.10008.5.1.4.1.1.4] Media Storage SOP Class UID\r\n" +
"00020003 UI #62 [1.3.46.670589.11.0.0.11.4.2.0.8743.5.5396.2006120114483570701] Media Storage SOP\r\n" +
"00020010 UI #20 [1.2.840.10008.1.2.1] Transfer Syntax UID";
System.out.println(s.replaceAll("(?m)^(\\d+).*", "$1"));
Output:
00020001
00020002
00020003
00020010
Upvotes: 1
Reputation: 8562
Simply match the rest of your string after the digits.
replaceAll(" UI.*","")
However, I would recommend matching the numerical id instead, it is cleaner and more robust in case your lines format would change
id = dcmObj.toString().matches("\d+");
Upvotes: 1