如何编写与包含数字的URL匹配的模式?

时间:2015-06-15 15:32:02

标签: ruby regex

我正在编写一个抓取工具,它必须遵循以下网址:

http://www.example.com/121-blah-blah
http://www.example.com/721-blah-blah
http://www.example.com/1512-blah-blah

但不是像以下网址:

http://www.example.com/category/1512-blah-blah

我应该如何编写模式来执行此操作?路径将以数字开头。

这是我到目前为止所得到的:

%r[(\d+]

3 个答案:

答案 0 :(得分:2)

将字符串解析为URI对象,并仅将正则表达式应用于路径部分。转换为URI增加了额外检查的好处,例如uri.absolute?。它还负责处理uri的各种可选部分,例如端口号或基本认证值。

uri = URI.parse(your_string)
uri.path =~ %r{^/\d+}

正则表达式中的^/确保匹配位于字符串的开头和前导斜杠之后。 \d+匹配任意数量的数字。如果您想在路径中的任何位置进行数字匹配,请使用:

uri.path =~ %r{\d+}

这样的语句会返回第一个匹配的整数意义索引,如果没有匹配则返回nil

您可能需要查看Rubular以调整正则表达式。

答案 1 :(得分:0)

如果数字将在“/”之后,那么正则表达式就像

一样简单
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="percent-mix"></div>
<!--Slider -->
<div class="mix-value"></div>
<!--Slider Value-->
<br />
<!--
<input id="range" type="range" min="0" max="100" /><output for="range"></output>
-->
<input type="button" value="click to append" />
<div id="the-grid">
  <ul class="align-table">
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
    <li class="t-align-row">
      <ul class="align-table">
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
        <li class="t-align">
          <div class="place"></div>
        </li>
      </ul>
    </li>
  </ul>
</div>

然后,您可以使用以下

将其与字符串匹配
regex = /\/\d+/

答案 2 :(得分:0)

如此有点具体:

http[s]?:\/\/[\w\.]+\/\d[\w-]*

See example