我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

用于验证URL中“ /”的正则表达式

I am writing a web application in golang. I am using regular expression to validate the URL. But I am not able to include '/' in the URL validation.

var validPath = regexp.MustCompile("^/(home|about)/(|[a-zA-Z0-9]+)$")

The above URL takes '/home/', '/about/' but could not make for '/'.

Could anyone please help me on this?

Why not adding that case as an alternative:

"^/$|(/(home|about)/(|[a-zA-Z0-9]+)$)"

You seem to want optional groups:

"^/(home|about)?(/[a-zA-Z0-9]+)?$"

Demonstration

近期文章

  • 在结构内部引用自己
  • Golang Sort为地图添加了额外的值
  • 解组接口类型
  • 执行:在循环中附加字节片[重复]
  • 从Golang中的字符串转换时间
  • Mgo是否缓存连接字符串?
  • 从Go编译器获取“未定义:距离”错误
  • Go语言中的静态类方法
  • 当设置为具有阻塞操作的单核时,Go中会实现并发性
  • Golang Go例行并发行为未按预期工作
  • Printf在golang的http处理程序中不起作用
  • 您可以解释func关键字和函数名称之间的参数吗?
  • Golang如何从html / template访问切片字段
  • 进行错误处理,使事情变干的惯用方式是什么
  • Go中的大int范围
  • 在Go中转换切片的一部分?
  • 如何在golang的不同文件中访问同一包中未导出的函数?
  • 引用子结构数组的Golang问题
  • 如何检测gorm中的连接失败?
  • CookieJar在哪里保留cookie?

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华