- to match a URL that is 1 level deep
- and a url (x)levels deep but under a specific directory
匹配深度为1级的URL
和url(x)级别深,但在特定目录下
How do I achieve this in regex?
我如何在正则表达式中实现这一目标?
I saw a regex solution from this question : Regular expression to match a URL with 6 or more levels
我从这个问题中看到了一个正则表达式解决方案:正则表达式匹配具有6个或更多级别的URL
But, I am unable to generate a working one for my 2 conditions above.
但是,我无法为上述两个条件生成一个有效的工作。
2 个解决方案
#1
1
Try this regex:
试试这个正则表达式:
^someserver.com/(\w+|specific(/\w+){n})$
Where n
is x-1
from your question
其中n是你问题的x-1
If you need separate regex:
如果你需要单独的正则表达式:
For one level:
一个级别:
^someserver.com/\w+$
For x levels under specific:
对于特定的x级别:
^someserver.com/specific(/\w+){x-1}$
#2
0
what if you just split it..?
如果你把它分开怎么办?
string url = "*.com/questions";
if (url.Split('/').Length == 2) //check for one level
{
System.Diagnostics.Trace.WriteLine("One Level");
}
if (url.Split('/')[0].ToLower() == "*.com") //check for base
{
System.Diagnostics.Trace.WriteLine("Good base");
}
may need to handle http:// though
可能需要处理http://
#1
1
Try this regex:
试试这个正则表达式:
^someserver.com/(\w+|specific(/\w+){n})$
Where n
is x-1
from your question
其中n是你问题的x-1
If you need separate regex:
如果你需要单独的正则表达式:
For one level:
一个级别:
^someserver.com/\w+$
For x levels under specific:
对于特定的x级别:
^someserver.com/specific(/\w+){x-1}$
#2
0
what if you just split it..?
如果你把它分开怎么办?
string url = "*.com/questions";
if (url.Split('/').Length == 2) //check for one level
{
System.Diagnostics.Trace.WriteLine("One Level");
}
if (url.Split('/')[0].ToLower() == "*.com") //check for base
{
System.Diagnostics.Trace.WriteLine("Good base");
}
may need to handle http:// though
可能需要处理http://