• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help On Java Regular Expression

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am struggling with this problem from sometime.
My Requirement is :
I have a list of url against which i want to filter all gif images using regular expression. but if my url ends with ma.gif, i dont want to filter it.

Sample :
URL : "/BS/myImage/ma.gif" -- don't filter
URL : "/BS/myImage/ma1.gif" -- filter
URL : "/BS/myImage/1ma.gif" -- filter
URL : "/BS/myImage/ma.1gif" -- don't filter

Here is the code that i came up with


This works but exactly opposite to my requirement(except for last URL) . I can use if(! matcher.find()) , but the problem is that i can't change Code, just the pattern configuration.

Any help on this would be appreciable.

Thanks in Advance!!
Regards,
Hemant


 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have a list of url against which i want to filter all gif images using regular expression. but if my url ends with ma.gif, i dont want to filter it.

Sample :
URL : "/BS/myImage/ma.gif" -- don't filter
URL : "/BS/myImage/ma1.gif" -- filter
URL : "/BS/myImage/1ma.gif" -- filter
URL : "/BS/myImage/ma.1gif" -- don't filter


But that last option doesn't end in ma.gif it ends in ma.1gif so can you please be more specific about exactly what is allowed.
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:
But that last option doesn't end in ma.gif it ends in ma.1gif so can you please be more specific about exactly what is allowed.



Yes, so the last option should not be filtered by the pattern.

In a nutshell, all ".gif" images except "ma.gif" should be filtered .

Regards,
Hemant

 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hemant Thard wrote:
In a nutshell, all ".gif" images except "ma.gif" should be filtered .



There still seems to be some ambiguity in your requirement but this seems to fit the bill.
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Richard.

This very well solved my problem.

Just tweaked a bit to make it case insensitive.



Regards,
Hemant>
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hemant Thard wrote:
Just tweaked a bit to make it case insensitive.

[code=java]
private static final String IMAGE_PATTERN = "(?i)(\\.gif$(?<!/ma.gif$))";



That 'requirements creep' could have been achieved by adding the CASE_INSENSITIVE flag when constructing the Pattern.

I still see an ambiguity; what do you want to do with the relative file name "ma.gif" that does not have a full path ( i.e. as opposed to one of the form "/xxx/yyy/ma.gif" )? The modification required to exclude this file is pretty much trivial so I will leave it up to you if you need to exclude it.
reply
    Bookmark Topic Watch Topic
  • New Topic