简体   繁体   中英

Parse string using regex in PHP

I had like to pass the following string via Regex to extract the domain name and the numeric value next to ms.

stackoverflow.com : [0], 96 bytes, 223 ms (223 avg, 0% loss)

I need:

stackoverflow.com , 223

Any help will be much appreciated.

Use following regex :

(\w+\.[a-zA-Z]{2,3}).+?(\d+)\s+ms

Explanation

  1. () : Capturing group
  2. \\w+ : Match any alphanumeric characters any number of times
  3. \\. : Match . literal
  4. [a-zA-Z]{2,3} : Match the two or three alphabets
  5. .+? : Matches any characters except linebreak
  6. \\d+ : Matches any number of digits

RegEx101 Demo

the following seems to work

(\w+\.\w+) : .*, ([0-9]+) ms

Tested here

Try this:

(\w+\.[a-zA-Z]{2,3}).+?(\d+)\s+ms

Should work.

Demo: https://regex101.com/r/qI0xN6/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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