• 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

Filename should contain spaces regex

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I want that my file should contain spaces
for example metlife newone.jif

Consider the following code



Here the output will be metlife
But I want it to be metlife newone.gif
If I remove space in between the image name..Output comes to be metlifenewone.gif...which is correct..

I guess I have to modify regex.Could anybody shed light on it..

Thanks in advance

[Edit - added code tags - MB]
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Raina wrote:I want that my file should contain spaces
for example metlife newone.jif


Any particular reason why? Spaces in filenames are a major PITA (not just for Java), especially in a *n[iu]x OS; and may well need conversion if you plan to access it as URL/URI.

Winston
 
Ryan Raina
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a requirement that image file can contain spaces in between them...I do support multiple platforms ...Could you suggest something...
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait, is it "should" contain spaces, as in "at least one space is required"? Or is it "can" contain spaces, as in "spaces are allowed"?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Raina wrote:We have a requirement that image file can contain spaces in between them...


I ask again: Why?

If there's a proper business case for it, then fine, but if it's simply because somebody "prefers" them, then I'd say you're buying yourself a world of hurt just to satisfy somebody's whim.

I do support multiple platforms...


More hurt then.

Could you suggest something...


Personally, I like underscores; but there are plenty of other possibilities (camel case?). If you absolutely must have spaces (see above), then just make sure that every single piece of code that has to deal with these files (scripts are usually the worst), is able to handle embedded spaces.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Raina wrote:I guess I have to modify regex.Could anybody shed light on it...


Well, leaving aside my rant about filenames that contain spaces (which I still reckon is a bad idea), both your regex and your buffer look wrong to me:ignores the first character after the '=' or '"' (the CASE_INSENSITIVE parameter is also redundant)
anddoesn't put quotes around your attributes.

My suggestion would be a pattern something like:
"src=['\"`]?([^'\"`]+)['\"`]?"
and a buffer of
String bufferString = "<img width=200 height=50 id=\"Picture 1\""
+ "src=\"metlife newone.gif\""
+ "alt=\"Description: http://www.metlife.co.in/grfx/header3a.gif\">";


However, I think you'd be much better off supplying the allowed characters in a filename in the centre portion of the pattern, rather than just saying that it can be anything except a quote, otherwise you may end up matching a lot more than you bargained for. I suggest you look at some of the '\p' character classes in the Pattern class docs.

HIH

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic