Regular Expressions: Match the documents ending with .pdf

Problem:

Create a regex expression that matches only documents ending with .pdf.

Output:






Solution:


I can't see an example importanter than this.

Step 1: Start with the ending. Tell the regex you want your the file to end with .pdf. To do that just type: \.pdf$ (^ indicates the beginning of the file, $ indicates the ending of the file).

Step 2: You don't care how the string begins and how many characters it will contain. So, you look for the regex note that indicates any regular character; it is already known \w.

Step 3: You don't know how many characters there are; it should be at least one character; the file cannot be ".pdf"; so for that you add + to the \w: \w+.

Step 4: Merge everything: \w+\.pdf$
That would capture for example: file_a_record_file.pdf

Step 5: The question is asking for file_a_record_file only! Not the pdf extension. No problem. Just do this: (\w+\)\.pdf$.
The paranthenties indicate that you're not interested in the .pdf thingy.

Final Answer: (\w+\)\.pdf$.


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact