MySQL Forums
Forum List  »  InnoDB

Re: Regex first three octets
Posted by: Gabe L
Date: December 01, 2008 05:45PM

Alfonso Caponi Wrote:
-------------------------------------------------------
> Hi,
>
> I've a column with ip address. How can I extract
> only the first three octets
> (for example 127.0.0)?
>
> select ip from tbl_hosts where ip REGEXP
> '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}';
>
> This query is wrong because extract all four
> octets.
>
> Thank you very much,
> Al


You need to escape the . character since it has meaning in a regular expression.

> select ip from tbl_hosts where ip REGEXP
> '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}';

select ip from tbl_hosts where ip REGEXP
'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';

Options: ReplyQuote


Subject
Views
Written By
Posted
5845
October 20, 2008 06:37AM
2845
October 20, 2008 08:08AM
2858
October 20, 2008 08:58AM
2951
October 21, 2008 03:32AM
Re: Regex first three octets
5753
December 01, 2008 05:45PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.