• 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

Regular Expression to extract quoted data.

 
Author
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have either of these strings...
I want a regular expression which uses a capture group to capture "uploaded-file 1.txt" EXCLUDING QUOTES. I don't care about the rest of the values.

I thought this would be trivial but I'm having a hard time getting the capture groups to work.

Suggestions?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand that correctly, you want to capture the text after filename=" up to but not including the next encountered double quote. If that's it, try this regex.
 
Michael Remijan
Author
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't know how you figured this out but thanks. I'll spend some time studying this to try and figure it out for myself.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets break it up and remove the backslashes needed for a Java String literal to make it easier on the eyes. Refer to the Pattern API and http://www.regular-expressions.info/

(?<=filename=") Non-capturing positive look-behind, matches the text filename"

([^"]*?) Capturing group. The character class [^"] matches any character except " and the *? repeats the match 0 or more times, reluctantly/lazily.

(?=")Non-capturing positive look-ahead, matches a ".

On review, the regex can be shortened by using the greedy construct. (?<=filename=")([^"]*) also does the job.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Giovanni Lima,
Your post was moved to a new topic.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic