duankuang7928 2015-01-12 15:56
浏览 33
已采纳

PHP Regex用于获取#和#之间的界限

I'm kind of stuck with a very simple regex. I've already tried a lot of thing but without result so I'll go with the simple :

I want to get a multiline result (for replace it later but I have already wrote this part and it works good) which supposed to be between '#' char. here the sample file :

# Zulu 9.8.9.8
add method 9.8.9.8 {ssh} {telnet}
add password 9.8.9.8 {jouejoue}
add user 9.8.9.8 {Zekrom}
# Zulu 1.1.1.1
add method 1.1.1.1 {ssh} {telnet}
add password 1.1.1.1 {mypassword}
add user 1.1.1.1 {adminstyle}
# Zulu 2.2.2.2
add method 2.2.2.2 {ssh} {telnet}
add password 2.2.2.2 {diffpassword}
add user 2.2.2.2 {pixyokadi}

I used 's' option like '/regex/s' so " " will be count in '.' char. I wanted to use [^#] but it cancelled the 's' option. I don't know why but when using this one :

/#.*#/s

It takes the 2 first paragraph ignoring the '#' at line '# Zulu 1.1.1.1' and I don't understand why. I've tried to use capture-parenthesis too without success. I used to search the IP with a regex but since I can't even get the paragraph I wanted, I used a more simple one. Could someone explain this to me ?

https://regex101.com/r/tI4hQ7/2

Thanks for reading.

  • 写回答

1条回答 默认 最新

  • doubaisui2526 2015-01-12 15:58
    关注

    Try with following regex:

    /#[^#]*/g
    

    g modifier does the global search.

    Also with [^#]* you will not capture next occurences of # - just everything else than # sign.

    Example:

    https://regex101.com/r/tI4hQ7/5

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真