|
Prev: urgent opening for database SQL Server developer with direct client in Bellevue,WA contract6months
Next: JNI - Passing and returning complex values
From: James on 3 Jul 2008 21:12 I`m trying to use regex to match/replace a word in parentheses. The regular expression private static final Pattern java_proc = Pattern.compile("(java)"); does not work, because parentheses are treated as groupings. Using "\" to designate the parentheses as literal characters does not work --- not sure why: private static final Pattern java_proc = Pattern.compile("\(java \)"); I searched for and read a related post here, but it did not help. I seem to be having a different problem than they. Or I just don`t understand the post. What am I doing wrong? Thanks, Alan
From: James on 3 Jul 2008 21:23 OK, I finally found the words about using double slashes in front of parentheses. So, now, why won`t the following regular expression pattern compile? private static final Pattern java_proc = Pattern.compile("\\\\.+\ \Process\\(java\\)\\"); The error says: java.lang.ExceptionInInitializerError Caused by: java.util.regex.PatternSyntaxException: Unknown character property name {r} near index 6 \\.+\Process\(java\)\ ^ This does not make sense to me. I`m trying to match text of the form (example): \\GOLLY\Process(java)\% Processor Time Thanks, Alan
From: Joshua Cranmer on 3 Jul 2008 21:31 James wrote: > OK, I finally found the words about using double slashes in front of > parentheses. So, now, why won`t the following regular expression > pattern compile? > > private static final Pattern java_proc = Pattern.compile("\\\\.+\ > \Process\\(java\\)\\"); > > The error says: > > java.lang.ExceptionInInitializerError > Caused by: java.util.regex.PatternSyntaxException: Unknown character > property name {r} near index 6 > \\.+\Process\(java\)\ > ^ This is what the regex is seeing. Don't forget that `\' is also a metacharacter in regexes. So to match a '\' in regex requires you to use '\\\\', which causes the regex to see '\\', which is what it uses to match as a '\'. So the regex you're probably trying to compile: "\\\\{2}.+\\\\Process\\(java\\)\\\\" (The {2} is so that you don't have to type in 8 slashes) -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
From: James on 3 Jul 2008 21:44 Thank you. I have one last remaining problem. The full data I`m working with, in CSV format, looks like this: "(PDH-CSV 4.0) (Eastern Daylight Time)(240)","\\GOLLY\Memory\% Committed Bytes In Use","\\GOLLY\Process(java)\% Processor Time" I want to match on \\GOLLY\Process(java)\ so I can replace it. The regular expression \\\\{2}.+\\\\Process\\(java\\). matches, but it matches too much of it: \\GOLLY\Memory\% Committed Bytes In Use","\\GOLLY\Process(java)\ How can I get it to only match the part I want? Thanks again, Alan
From: Joshua Cranmer on 3 Jul 2008 21:52
James wrote: > The regular expression > > \\\\{2}.+\\\\Process\\(java\\). > > matches, but it matches too much of it: In that case, you probably want this regex: \\\\{2}[^\\\\]+\\\\Process\\(java\\) -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth |