我想使用正则表达式获取以下字符串的子字符串。 [英] I want to get the sub string for the below string using regex.

查看:68
本文介绍了我想使用正则表达式获取以下字符串的子字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入字符串 -

 GlobalLib:// Library6 / Master Copies / New folder_1 / New folder_2 / PLC_4 



OutPut -

新文件夹_1 /新文件夹_2 



同时验证输入字符串格式 -

 GlobalLib:// 

是固定前缀

Library6是库名的变量。

主副本

是固定名称。 />

新文件夹_1 /新文件夹_2 

这是文件夹路径层次结构。

PLC_4是库项目名称。

这是一个搜索逻辑,我希望从给定路径匹配我们的项目。

1.使用正则表达式我必须找到可变的文件夹路径,根据给定的字符串,这是

新文件夹_1 /新文件夹_2 



2.我还要使用正则表达式验证输入字符串。



我尝试过的事情:



需要你身边的帮助。

我试过了来自网络的多个场景但无法找到适合此p的任何场景roblem。

解决方案

下面的代码用于验证整个语句。我已经使用组来验证字符串的每个部分。请参阅:



  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>。*)

;

匹配rm = Regex.Match(s,pattern);
foreach (组grp in rm.Groups)
{
Console.WriteLine( 组名:{0},grp.Name);
Console.WriteLine( 值:{0},grp.Value);
Console.WriteLine();
}





因此,如果您只想返回文件夹组,请使用:

  var  folders = rm.Groups [ 文件夹]; 
Console.WriteLine( {0},folders.Value);
// 返回:新文件夹_1 /新文件夹_2 /

< br $>


类似地,获取库名:

  var  lib = rm.Groups [  libraryname]; 
Console.WriteLine( {0},lib.Value);





要检查整个字符串是否与模式匹配,请使用:

 < span class =code-keyword> var  valid = Regex.IsMatch(s,pattern); 
Console.WriteLine( {0},有效);

 / [^ /] +?/ [^ /] +?(?= / [^ /] +?

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.

解决方案

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

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:

var folders= rm.Groups["folders"];
Console.WriteLine("{0}", folders.Value);
//returns: New folder_1/New folder_2/



In analogy, to get a library name:

var lib= rm.Groups["libraryname"];
Console.WriteLine("{0}", lib.Value);



To check if entire string is matching to the pattern, use:

var valid = Regex.IsMatch(s, pattern);
Console.WriteLine("{0}", valid);


Try this:

/[^/]+?/[^/]+?(?=/[^/]+?


这篇关于我想使用正则表达式获取以下字符串的子字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆