Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Input string -
GlobalLib://Library6/Master Copies/New folder_1/New folder_2/PLC_4

OutPut -
New folder_1/New folder_2

Also verify the input string format -
GlobalLib://
is fixed prefix
Library6 is variable with library name.
Master Copies
is fixed name.
New folder_1/New folder_2
this is folder path hierarchy.
PLC_4 is library item name.
this is a search logic which i want to match with our project from the given path.
1. using regex i have to find the folder path which is variable and as per the given string this is
New folder_1/New folder_2

2. Also i want to verify the input string using regex.

What I have tried:

Need help from your side.
I have tried multiple scenarios from net but not able to find any scenarios which is fit for this problem.
Posted
Updated 11-Feb-19 3:33am
Comments
Richard MacCutchan 11-Feb-19 7:55am    
Probably easier just to use String.Split and String.Equals.

Below code is used to validate entire statement. I've used groups to validate each part of string. See:

C#
string s = "GlobalLib://Library5/Master Copies/New folder_1/New folder_2/PLC_3";
string pattern = @"^(?<prefix>GlobalLib://Library\d{1,})(?<constant>/Master Copies/)(?<folders>.*/)(?<libraryname>.*)$";

Match rm = Regex.Match(s, pattern);
foreach(Group grp in rm.Groups)
{
	Console.WriteLine("Group name: {0}", grp.Name);
	Console.WriteLine("Value: {0}", grp.Value);
	Console.WriteLine();
}


So, if you would like to return only "folders" group, use this:
C#
var folders= rm.Groups["folders"];
Console.WriteLine("{0}", folders.Value);
//returns: New folder_1/New folder_2/


In analogy, to get a library name:
C#
var lib= rm.Groups["libraryname"];
Console.WriteLine("{0}", lib.Value);


To check if entire string is matching to the pattern, use:
C#
var valid = Regex.IsMatch(s, pattern);
Console.WriteLine("{0}", valid);
 
Share this answer
 
v2
Comments
Member 8029956 12-Feb-19 6:41am    
Library5 is a name of the library folder, that can be any name there would be no fixed format, then what would be the solution regex for that?
Member 8029956 12-Feb-19 7:22am    
I am able to find that i think this should be
string pattern = @"^(?<prefix>GlobalLib://)(?<libraryname>.*)(?<constant>/Master Copies/)(?<folders>.*/)(?<libraryobjectname>.*)$";

in this regex if i put /// after prefix then last /come to under library name but my expectation library name should not contain / it its name.
String -
string s = "GlobalLib:///Library5/Master Copies/New folder_1/New folder_2/PLC_3";
Prefix is - GlobalLib://
Library name - /Library5
Constant - /Master Copies/
Folder path - New folder_1/New folder_2/
Library Object name - PLC_3
All are fine only library name should be Library5 not /Library5, in that case this should give some error.
Maciej Los 12-Feb-19 7:38am    
Looks perfect to me.
Member 8029956 12-Feb-19 7:41am    
if my string is "GlobalLib:///Library5/Master Copies/New folder_1/New folder_2/PLC_3" then Library name - /Library5 but i want library name should be only "Library5" not "Library5" and it should fail if library name contains any /. Is it possible to check this in same regex or i have to create new regex to check library that does not contain any "/"?
Maciej Los 12-Feb-19 7:55am    
If [GlobalLib] ends with three times [///], then change this: (?<prefix>GlobalLib://) to: (?<prefix>GlobalLib:///). In this case a library name will not return [\Library5] but [Library5]. Check it out.
Try this:
/[^/]+?/[^/]+?(?=/[^/]+?$)
If you are going to play with Regxes, get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
Maciej Los 11-Feb-19 9:36am    
Good, but does not meet all OP's criteria, which were defined:
1. using regex i have to find the folder path which is variable and as per the given string this is
2. Also i want to verify the input string using regex.
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