0

I am using this RegEx:

\s*((\w*\.*\s*)+[.*(\w)]+(?=\d{4}))(\d{4}\s*\,)*

And the goal is to match words with the last one that ends with a dot, follower with 4 digits ending with a comma.

This is a test.2014,

And it works fine. Now, I would like to add the possibility to have a whitespace (\s) between the "test." and "2014,", but the whitespace is not a mandatory parameter, it should be matched if there.

Could you please help me on how to add that to my regex? How can we set not mandatory parameters ?

Thank you.

2
  • I'm pretty sure that [.*(\w)]+ doesn't match what you think it does. Commented Feb 13, 2014 at 15:06
  • The regex matches ...... or (.*(()... Commented Feb 13, 2014 at 15:10

4 Answers 4

1

Try this pattern

\s*((\w*\.*\s*)+[.*(\w)]+\s*(?=\d{4}))(\d{4}\s*\,)*

\s* there is a zero or more time space. I added it before date pattern

Or try:

\s*((\w*\.*\s*)+[.*(\w)]+(?=\s*\d{4}))(\s*\d{4}\s*\,)*
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Victor. Could you please give more details on what you did?
1

There are a couple ways you can do this. You can use the question mark, an asterisk, or you can specify the range of viable occurrences as is shown in the table below.

Regular Expressions Quantifiers
*   0 or more
+   1 or more
?   0 or 1
{3} Exactly 3
{3,}    3 or more
{3,5}   3, 4 or 5

\s*((\w*\.*\s*)+[.*(\w)]+(\s+)(?=\d{4}))(\d{4}\s*\,)*
\s*((\w*\.*\s*)+[.*(\w)]+(\s*)(?=\d{4}))(\d{4}\s*\,)*
\s*((\w*\.*\s*)+[.*(\w)]+\s{0,1}(?=\d{4}))(\d{4}\s*\,)*

4 Comments

thanks, now that I've added multiple solutions, mind removing your comment?
I removed message. What does you meant about mind?
Milos, you may also find Rubular helpful in parsing your regular expressions. If Rubular doesn't work for you, there are plenty of other helpful online regexp parsers.
Victor - "mind" is like saying, "would it trouble you to ...", or "would you please ...". I guess it really comes from the idea of asking someone to be mindful of your request - or something to that effect.
1

And the goal is to match words with the last one that ends with a dot, follower with 4 digits ending with a comma

(?:\s*\w+)*\s*\w+\.\d{4},

Now, I would like to add the possibility to have a whitespace (\s) between the "test." and "2014,", but the whitespace is not a mandatory parameter,

(?:\s*\w+)*\s*\w+\.\s*\d{4},

Comments

0

Try this

/\s*((\w*\.*\s*)+[.*(\w)]+(\s*)(?=\d{4}))(\d{4}\s*\,)*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.