regex - 正则表达式匹配组号
问题描述
我正在学习正则表达式并遇到了问题。我有以下内容:
href="http://google.com/topic/713725-dogs-are-great-3"> href="http://google.com/topic/213225-humans-are-great"> href="http://google.com/topic/342315-cats-are-great-but-small">
使用此代码
href="(?:[^"]*)topic/([^<]*)">
我可以选择
713725-dogs-are-great-3 213225-humans-are-great 342315-cats-are-great-but-small
但我只想匹配数字
342315 213225 713725
有任何想法吗?
解决方案
使用您显示的示例和尝试,请尝试以下正则表达式;这将创建 1 个捕获组,您可以使用它来获取匹配的值。
\bhref="(?:[^"]*)topic\/(\d+)-.*">$
说明:为上述添加详细说明。
\bhref=" ##Matching href using \b before it as a word boundary followed by =" here.
(?:[^"]*)topic\/ ##In a non-capturing group matching till " everything till topic/
(\d+) ##Creating 1st capturing group which has digits in it.
-.*">$ ##Matching everything till " followed by "> till end of value.
推荐阅读
- facebook - 通过 API 创建的直播流的 Facebook 隐私设置
- stm32 - STM32F4 用中断从 uart 接收数据的问题
- apache-kafka - 从 StateStore 更改日志主题具体化 GlobalKTable 并使用exact_once 处理时-其他主题消费者停止工作
- java - Mapbox Android中空对象引用上的NavigationView
- java - SSL peer 错误关闭 - 阿里巴巴 OSS java SDK
- python - 逻辑与,与带有浮点数的 numpy nan 进行比较
- python - Django Rest 框架中的 OTP 验证
- php - 从 laravel 中的选项选择中获取名称和值
- javascript - 使用nodeJS创建依赖下拉菜单过滤器
- reactjs - 酶的导入组件在浅层上抛出一个错误的意外标记(它在 Typescipt 上)