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