简体   繁体   English

RegEx使用php替换字符串结尾

[英]RegEx Replace at end of string using php

I try to read chemical formulas like C6H12-1, C6H12-1(co2) using 我尝试使用读取化学式,例如C6H12-1,C6H12-1(co2)

"/(?i)\b[a-z]+(?:\d+[a-z]+)*\b/"

$fuel_value = preg_replace("/(?i)\b[a-z]+(?:\d+[a-z]+)*\b/","",$fuel);
print_r(implode($fuel_value));

It prints C6-H5(CH3) 15.0 ! 它打印C6-H5(CH3)15.0! ( ) as output, but i only want to read C6-H5(CH3) how can i eliminate data after space ie 15.0 ! ()作为输出,但是我只想读取C6-H5(CH3),如何在空格后即15.0后消除数据! ( ) ()

I tried to use $ but i failed. 我尝试使用$,但失败了。 I am working on php for first time. 我是第一次使用php。

Apologize if my question is silly 道歉,如果我的问题很傻

I would use this: 我会用这个:

FUEL ([\w\(\)-]+)

Here's a demo . 这是一个演示

i use preg_match_all instead of preg_replace and list chemical formula's 我使用preg_match_all而不是preg_replace并列出了化学式

$str = "FUEL C6H12-1 0.0 ! Fuel Fraction oFUEL C6H12-1 0.0 ! Fuel Fraction of Total Fuel Species(molefr) FUEL C6H5CH3 15.0 ! Fuel Fraction of Total Fuel Species(molefr) FUEL IC8H18 72.5 ! Fuel Fraction of Total Fuel Species(molefr) FUEL NC7H16 12.5 ! Fuel Fraction of Total Fuel Species (molefr) FUEL T124MBZ 0.0 ! Fuel Fraction of Total Fuel Species (molefr)";

preg_match_all("/[A-Z]+\d.*?[\s]/",$str,$match);

print_r($match);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM