Hyder B.
Hyder B.

Reputation: 12296

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.

Upvotes: 0

Views: 39

Answers (3)

Try this:

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

Should work.

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

Upvotes: 1

urban
urban

Reputation: 5702

the following seems to work

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

Tested here

Upvotes: 1

Tushar
Tushar

Reputation: 87233

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

Upvotes: 2

Related Questions