doulao2128 2011-01-28 16:14
浏览 45
已采纳

正则表达式,如果它不包含特定字符,则获取完整字符串?

I need help with a regular expression that will find matches in the strings below:

myDOG_test

myCAT_test

Basically, I want to return 'DOG' or 'CAT' from these paths.

Then I have similar strings (all start with 'my') that don't contain the underscore AFTER the value I want, and in that case I just want to return the FULL string -- in a match group.

myCentralReports

myDEMO3

This is the REGEXP that I have so far:

.*?my(.*?)\_.*

This correctly puts CAT & DOG in the matching group, but I'm having problems matching the other 2 strings. Obviously I left the hardcoded underscore in there just to show you what I started with -- but I need to modify this for the other case. Any help is appreciated! Thanks.

  • 写回答

3条回答 默认 最新

  • douchuopiao9002 2011-01-28 16:27
    关注
    '/\smy(.+?)[_|\s]/'
    

    This will get anything between a whitespace character followed by "my", and the next trailing underscore or whitespace character. try it out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办