# Extract URL from Text Field into a New Field Called URL

**URL:** <https://discuss.elastic.co/t/extract-url-from-text-field-into-a-new-field-called-url/34100>\
**Category:** Logstash\
**Created:** [November 8, 2015, 8:28am UTC](https://discuss.elastic.co/t/extract-url-from-text-field-into-a-new-field-called-url/34100 "2015-11-08T08:28:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isra](https://avatars.discourse-cdn.com/v4/letter/i/7ba0ec/32.png) [@Isra](https://discuss.elastic.co/u/Isra)\
**Post date:** [November 8, 2015, 8:28am UTC](https://discuss.elastic.co/t/extract-url-from-text-field-into-a-new-field-called-url/34100/1 "2015-11-08T08:28:41Z")

</div>

I'm inputting a field called `text`. this field may at times contain a URL.What I would like to do is extract the URL's from `text`, and put them in a new field called `URL`.

I tried grok, but it seems like grok patterns need a specific log format in order for it to work. For an example, the following will work:

```
5546 hello www.google.com
{id} {text} {URL}

```

But the following wouldn't

```
4324 hello my name is Ryan www.yahoo.com
{id} {text} {URL}

```

instead, it would take hello as text, and not take [www.yahoo.com](http://www.yahoo.com) as the URL. Is there a way around this? Please note that sometimes, the text might look like the following:

```
www.gmail.com hello everyone 

```

What filter can I use in order to extract the URL from the text coming into Logstash?

Thank you.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 8, 2015, 8:45pm UTC](https://discuss.elastic.co/t/extract-url-from-text-field-into-a-new-field-called-url/34100/2 "2015-11-08T20:45:53Z")

</div>

The grok filter is the right tool for the job. Since the position of the URL isn't the same all the time you can't use a grok expression that extracts word _N_ from the input text. Instead you need to construct a pattern that recognizes the URL itself, i.e. by looking for a word that starts with "www.", or perhaps any space-delimited sequence of characters that begins with something that looks like a domain name. For example,

```
(?<url>[a-z0-9-]+\.[a-z0-9-]+\S+)

```

captures into the field `url` anything that begins like a hostname with at least two labels ("[www.google.com](http://www.google.com)", "[google.com](http://google.com)" but not "com") followed by any number of non-space characters.

(Strictly speaking I believe a URL needs to begin with a scheme, like http://.)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 5:23am UTC](https://discuss.elastic.co/t/extract-url-from-text-field-into-a-new-field-called-url/34100/3 "2017-07-06T05:23:27Z")

</div>


