Re: Matching parentheses with Regular Expressions
- From: Mark Space <markspace@xxxxxxxxxxxxxx>
- Date: Fri, 04 Jul 2008 11:36:12 -0700
shakah wrote:
On Jul 3, 9:52 pm, Joshua Cranmer <Pidgeo...@xxxxxxxxxxxxxxx> wrote: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
FWIW, you could avoid a little of the backslash escape mess
by using single-char character classes, e.g.:
Pattern.compile("[\\]{2}[^\\]+[\\]Process[(]java[)]") ;
// ...outside of a Java string that'd be [\]{2}[^\]+
[\]Process[(]java[)]
You also might get rid of some of those backslashes by substituting another character, then using replace() on the string before compiling it.
final static String PATTERN = "``{2}.+``Process`(java`)";
String myRegex = PATTERN.replace("`", "\\" );
System.out.println( myRegex );
Result:
\\{2}.+\\Process\(java\)
It just makes things more readable. Using `, or %, or # in a string, then replace that character with \'s before compiling it as a regex can save your eyes.
Incidentally, I wonder if Sun could be convinced to add this themselves. Maybe add a new operator/keyword altogether. Like # introduces new keywords or operators. It's followed by the keyword or operator. This just allows Sun to make new keywords or operators, with out breaking any existing code. So #s might give us new string constatns. Let's say ' then means like a Unix shell string, where escaping is ignored.
String regex = #s'\\{2}.+\\Process\(java\)';
Would give that literal string, without the need to escape the backslashes. Easier for regex at least. Other types of flags besides ' could be introduced too. `,$,@,%,= might do the same thing, just use a different character as a string terminator, in case you want a ' to be part of the string. """ might introduce a "here-is" operator. Etc.
Just thinking out loud....
.
- Follow-Ups:
- Re: Matching parentheses with Regular Expressions
- From: Roedy Green
- Re: Matching parentheses with Regular Expressions
- References:
- Matching parentheses with Regular Expressions
- From: James
- Re: Matching parentheses with Regular Expressions
- From: James
- Re: Matching parentheses with Regular Expressions
- From: Joshua Cranmer
- Re: Matching parentheses with Regular Expressions
- From: James
- Re: Matching parentheses with Regular Expressions
- From: Joshua Cranmer
- Re: Matching parentheses with Regular Expressions
- From: shakah
- Matching parentheses with Regular Expressions
- Prev by Date: Re: easy way to spank the monKey
- Next by Date: Re: Matching parentheses with Regular Expressions
- Previous by thread: Re: Matching parentheses with Regular Expressions
- Next by thread: Re: Matching parentheses with Regular Expressions
- Index(es):
Relevant Pages
|