Regular Expressions: Capturing the date

Problem:

Using regular expressions, first detect the text; then capture the date according to the output.

Output:






Solution:


Part A: Detects the text

\w: designates a string
\d: designates a number
\s: designates a whitespace

\w{1,3}: designates three characters.
\d{1,4}: designates a four numbers.
\s: designates a white space.

Merging: \w{1,3}\s\d{1,4}

That would detect the text perfectly but not capture what is demanded, look:



Part B: Captures the date

We have:

\w{1,3}\s\d{1,4}

To capture what you want, simple add parentheses.

First the date:

\w{1,3}\s(\d{1,4})

Then the whole expression:

(\w{1,3}\s\d{1,4})

Now, you have to merge both:

(\w{1,3}\s(\d{1,4}))

Works:


Alternative solution:
(\w+ (\d+))


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