V
V
Vladimir Grabko2016-07-23 15:56:43
go
Vladimir Grabko, 2016-07-23 15:56:43

How to find 1 domain from a string?

I have a string that contains a domain in the format: domen.com I want to find 1 domain from the text (continue searching until the first occurrence). Googled. could you help me?
expecting hgh.ru but getting h.ru

package main

import(
  "fmt"
  "regexp"
)
var re = regexp.MustCompile("[a-zа-я]\\.[a-zа-я]{2,6}")
func main(){
  for _, value := range re.FindAllString("ghh  hgh.ru", -1) {
    fmt.Println(value)
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
fastpars, 2016-07-23
@VGrabko

search until the first occurrence: FindStringSubmatch FindSubmatch
`
[
a-za-z]+\.[a-za-z]{2,6}` com/library/view/reg... or here myregexp.com/examples.html + add our [a-z]

N
nelolka, 2016-07-23
@nelolka

And what domain zone should be? If there are several, then you need to list them.

O
Oleg, 2016-07-23
@politon

<?php
$a    = 'domen.com';
$b = 'Домен site.ru';//сюда вкидываем строку
$c = stripos($b, $a);
if ($c === false) {
    echo ("Домена '$a' нет в строке");
} else {
     echo("Домен '$a' есть в строке");
} 
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question