A Regular expression to find the letter 'v' followed by any amount of numbers and possibly a dot

I need a regular expression to find something in a string so that it can be removed.

I need to find when there is something like V100, v1, V1.0, or v10.0123, so the letter 'v' upper or lower case, followed by any number of numbers that could possibly have a '.' in it, but it being sandwiched between other characters should not find it.

Example:

words v10 - it should find v10
wordsv1 - it should not find v1
v1words -it should not find v1

I'm new to regular expressions, so if you could please explain what each part of the expression means that would be very helpful thanks!
LVL 3
buttonMASTERAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
I think you could use this to return the matching text you are looking for:

\s(v[0-9.]*)\s

Open in new window


Here is a great online tool for poking at and learning regex, and it shows a breakdown of each piece of the regular expression.  Take a look and come back with questions you still have.



»bp
gr8gonzoConsultantCommented:
Bill is almost right.

Try this:
\b([vV][0-9\.]+)\b

Open in new window


Explanation:
1. I swapped out \s for \b, because \s is for whitespace (and there's no guarantee that there will be whitespace around the value), while \b is for word boundaries, so it won't find "abcv1def" but it will find "Hello world.v1" and similar instances where you want to ensure you're not matching in the middle of a bigger word.

2. I changed the * to + so it would require at LEAST one number or dot (so "v" alone would not match).

3. I changed the . to a \. because . by itself means "any character" while \. means "the dot character"

4. I changed "v" to "[vV]" to look for upper or lower case V letters. You could also just use the case-insensitive flag on your method, too. Either way would handle the case issue.
Rgonzo1971Commented:
Hi,

Since the dot cannot be after v then try
1:\b([vV]\d[\d\.]*)\b

Open in new window

REgards
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Bill PrewIT / Software Engineering ConsultantCommented:
Good on upper and lower V's, I didn't catch that on first read.  And I like the 1 or more better than 0 or more, agreed.  But inside a character list [0-9.] I'm pretty sure there is no need to escape the period.  Sure it works, but not needed...

It's hard to know from the question description if \b or \s makes more sense.  I went more conservative, since it felt like xxxx-v1 should not match, but it depends on authors specific needs.

So I'll land at:

\s([vV][0-9.]+)\s

Open in new window



»bp
Bill PrewIT / Software Engineering ConsultantCommented:
Well, we don't know that v.10 is not valid, do we?


»bp
Rgonzo1971Commented:
then try
\b([vV]\d+(\.[\d]+)?)\b

Open in new window

buttonMASTERAuthor Commented:
Hi everyone. Thank you for your answers. I'm currently going through everything to see what works for my use cases.

Well, we don't know that v.10 is not valid, do we?
v.10 would not be valid, thanks for checking.
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, here's an adjusted version of mine, based on discussion here.  And some additional test cases.

Try changing the \s to \b (two of them) in the regex to see the difference, then choose based on your needs.

https://regex101.com/r/FDr6GN/2


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
buttonMASTERAuthor Commented:
Thank you all for your help.

Bill, you were so helpful with those links and accounting for v1.2.3 was great. gr8gonzo good job for reading the entire question and explaining things. And Rgonzo1971 you answer was correct for what I described in my question, but Bill caught cases I didn't think about.
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that was helpful.


»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.