Click here to Skip to main content
16,020,080 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I need to validate a string using regex, string is as below where wording can be different (also in length) but the most important things are the . and _

GOOD
FW2.PLANT_DEV.APPNAME.REQUEST.*.*
FW2.PLANT_DEV.APPLICATION.REQUEST.*.*
FW2.PLANT_DEV.APPLICATION.REPLY.*.*
FW2.PLANT_PRD.APPNAME.REQUEST.*.*
FW2.PLANT_PRD.APPNAME.REQUEST.TYPE.*
FW2.PLANT_PRD.APPNAME.REQUEST.TYPE.NAME-001
FW2.PLANT_PRD.APPNAME.REQUEST.*.NAME
PLANT_DEV.APPLICATION.REQUEST.*.*
PLANT_DEV.APPLICATION.REPLY.*.*
PLANT_PRD.APPNAME.REQUEST.*.*
PLANT_PRD.APPNAME.REQUEST.TYPE.*
PLANT_PRD.APPNAME.REQUEST.TYPE.NAME
PLANT_PRD.APPNAME.REQUEST.*.NAME

NOT GOOD
FW2_SITE_DEV.APPNAME.REQUEST.*.*
FW2_PLANT_DEV.APPNAME.REQUEST.*.*
FW2.SITE.DEV.APPNAME.REQUEST.*.*
FW2.SITE_DEV.APPNAME.REQUEST
B2K_DEV.APPNAME.REQUEST.*.*
B2K.DEV.APPNAME.REQUEST.*.*


Any help on this please?

What I have tried:

Nothing yet that result in a succesfull regex expression

([a-zA-Z0-9]+.{1})?\w*_{1}([^_\W]*.{1})([^_\W]*.{1})([^_\W]*.{1})(\*.{1})(\*{1})
Posted
Updated 22-Nov-18 23:40pm
v4

It depends on exactly what you expect in the string: the stuff that determines validity / invalidity.
And a vague "the most important things are the . and _" isn't very specific, given the single example contains four '.' characters and a single '_'
But, assuming order and only the first '.' is important:
^\w+?\.\w+_[\w\.\*]+$

Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
[no name] 23-Nov-18 4:29am    
Which expression do you need? What is your expected output ?
datt265 23-Nov-18 4:45am    
GOOD
FW2.PLANT_DEV.APPNAME.REQUEST.*.*
FW2.PLANT_DEV.APPLICATION.REQUEST.*.*
FW2.PLANT_DEV.APPLICATION.REPLY.*.*
FW2.PLANT_PRD.APPNAME.REQUEST.*.*
FW2.PLANT_PRD.APPNAME.REQUEST.TYPE.*
FW2.PLANT_PRD.APPNAME.REQUEST.TYPE.NAME-001
FW2.PLANT_PRD.APPNAME.REQUEST.*.NAME
PLANT_DEV.APPLICATION.REQUEST.*.*
PLANT_DEV.APPLICATION.REPLY.*.*
PLANT_PRD.APPNAME.REQUEST.*.*
PLANT_PRD.APPNAME.REQUEST.TYPE.*
PLANT_PRD.APPNAME.REQUEST.TYPE.NAME
PLANT_PRD.APPNAME.REQUEST.*.NAME

NOT GOOD
FW2_SITE_DEV.APPNAME.REQUEST.*.*
FW2_PLANT_DEV.APPNAME.REQUEST.*.*
FW2.SITE.DEV.APPNAME.REQUEST.*.*
FW2.SITE_DEV.APPNAME.REQUEST
B2K_DEV.APPNAME.REQUEST.*.*
B2K.DEV.APPNAME.REQUEST.*.*
OriginalGriff 23-Nov-18 5:29am    
You are going to have real problems doing this with a regex:
PLANT_DEV.APPLICATION.REQUEST.*.* is allowed
B2K_DEV.APPNAME.REQUEST.*.* is not allowed.
There is no generic solution that can see a difference between them without considering lengths, and then
FW2.PLANT_DEV.APPLICATION.REPLY.*.* is allowed, so lengths may not help.

I think you will need to look at your potential data a lot more closely, before you can can up with any scheme that will work reliably for all cases. I;'d start with the source and find out what it expects to generate.
[no name] 23-Nov-18 5:03am    
I think you will not get any specific regex for this. You have to generate regex as per your requirement. There are many websites to generate regex. You can google.
NOT GOOD
FW2_SITE_DEV.APPNAME.REQUEST.*.*
FW2_PLANT_DEV.APPNAME.REQUEST.*.*
FW2.SITE.DEV.APPNAME.REQUEST.*.*
FW2.SITE_DEV.APPNAME.REQUEST
B2K_DEV.APPNAME.REQUEST.*.*
B2K.DEV.APPNAME.REQUEST.*.*

It is a good idea to tell the reason why those are not good.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900