• 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

Regex to find a string anywhere

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

Apologies if this is not worded well..

Basically I have this line in a servlet


where empid is a string.

So currently I am getting files with the empid as the name, but I need to get files that have the empid ANYWHERE in the name.. so for example if my empid is 2500, I need to get the file if its called anything like -
2500_Amy
Amy_2500
Amy2500
2500Amy

Can anyone help with this? Is it regex I need to use and if so what do I write? Thanks
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know much about regexes, but what about this?

.*2500.*

Remember that the full stop is a metacharacter for anything except line ends.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if it's as simple as finding a String in a String, what about using String#indexOf(String)?  Anything that doesn't return -1 is a match.

If you need something more complex than that, you could use Matcher#find().
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or use String.contains(substr)
 
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you guys have the requirements mixed up. This is not about finding a string, this is about finding a file.

The Files utility class contains a method that allows you to iterate children of a specified directory while filtering them using a glob. Globs are like simple regular expressions that target file paths specifically.

If you can guarantee that the employee ID doesn't contain special glob characters, you can use it directly, without calling escapeSpecialGlobCharacters(). Otherwise you can use the following implementation:

This method escapes the special characters * ? { } [ ] \ with a backslash.
 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I question the wisdom of accessing files on the server's file system like that though.

Are you sure that the application will always run on the same machine as XAMPP, with XAMPP being installed in that exact location, with that exact directory structure? If the images are hosted by XAMPP, isn't it better to retrieve them using a HTTP request to localhost?
 
Derv McDermott
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah so I've now realised that you can't use regex directly with FileInputStream..

Stephan van Hulst thank you but I am a bit confused on where I'd put your given code in my servlet.

Also it is not actually XAMPP that will be used that was just for purpose of an example, but the photos will actually be in a folder on a Wildfly server (which my app will be hosted on) and they will stay in that folder.. if that makes my question any different? Is there a better way you think I could be reading the files to begin with?

Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Derv McDermott wrote:Stephan van Hulst thank you but I am a bit confused on where I'd put your given code in my servlet.


In the same place where you use your existing code.

Is there a better way you think I could be reading the files to begin with?


Normally I wouldn't use a file system to store dynamic resources in the first place, but a database instead. You can use the BLOB data type.

Are the images shared between applications? Will they always be in a fixed location relative to your application? Can you guarantee this for all customers where the application will be installed?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic