RegEx模式可检测两个连续的数字,后面最多9个字符?

时间:2018-11-26 14:17:31

标签: java regex

如何在Java正则表达式中创建一个正则表达式来检测任何字符串,该字符串以两个连续的数字开头,然后是最多9个带有连字符的连续字符?例如:

============= logstash @ logstash.host.example.com : ~ ============
$ cfg="$(cat)"
input { stdin {} }
filter { if [message] =~ /^[0-9]*$/ { uuid { target => "uuid" } } }
output { stdout { codec => "rubydebug" } }
============= logstash @ logstash.host.example.com : ~ ============
$ /usr/share/logstash/bin/logstash --config.string "$cfg" --pipeline.workers 1 --log.format json --path.data /tmp/kadmar
WARNING: Could not find logstash.yml which is typically located in $LS_HOME/config or /etc/logstash. You can specify the path using --path.settings. Continuing using the defaults
Could not find log4j2 configuration at path /usr/share/logstash/config/log4j2.properties. Using default config which logs errors to the console
[WARN ] 2018-11-26 14:50:36.434 [LogStash::Runner] multilocal - Ignoring the 'pipelines.yml' file because modules or command line options are specified
[INFO ] 2018-11-26 14:50:37.646 [LogStash::Runner] runner - Starting Logstash {"logstash.version"=>"6.3.0"}
[INFO ] 2018-11-26 14:50:44.490 [Converge PipelineAction::Create<main>] pipeline - Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>1, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50}
[INFO ] 2018-11-26 14:50:44.840 [Converge PipelineAction::Create<main>] pipeline - Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x4620459c run>"}
The stdin plugin is now waiting for input:
[INFO ] 2018-11-26 14:50:45.048 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[INFO ] 2018-11-26 14:50:45.457 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9601}
hello
{
       "message" => "hello",
    "@timestamp" => 2018-11-26T13:50:56.293Z,
          "host" => "logstash.host.example.com",
      "@version" => "1"
}
ab123cd
{
       "message" => "ab123cd",
    "@timestamp" => 2018-11-26T13:51:13.648Z,
          "host" => "logstash.host.example.com",
      "@version" => "1"
}
123
{
       "message" => "123",
          "uuid" => "3cac8b35-6054-4e14-b7d0-0036210c1f2b",
    "@timestamp" => 2018-11-26T13:51:18.100Z,
          "host" => "logstash.host.example.com",
      "@version" => "1"
}
1
{
       "message" => "1",
          "uuid" => "1d56982f-421a-4ccd-90d6-6c2c0bcf267d",
    "@timestamp" => 2018-11-26T13:51:25.631Z,
          "host" => "logstash.host.example.com",
      "@version" => "1"
}

{
       "message" => "",
          "uuid" => "747ac36f-8679-4c66-8050-9bd874aef4c5",
    "@timestamp" => 2018-11-26T13:51:27.614Z,
          "host" => "logstash.host.example.com",
      "@version" => "1"
}
012 456
{
       "message" => "012 456",
    "@timestamp" => 2018-11-26T13:52:09.614Z,
          "host" => "logstash.host.example.com",
      "@version" => "1"
}

nnccccccccc-nnccccccccc

nncccccc-nnccccccccc

其中nnccccccc-nncccccccc 代表0到1的数字,n代表字母字符。

到目前为止,我已经尝试过:https://regex101.com/r/a1eJvY/2

4 个答案:

答案 0 :(得分:1)

您可以使用X&示例:https://regex101.com/r/A2wiHH/2

这将匹配如下所述的字符串:

  1. 字符串的开头
  2. ^(\d{2}[a-zA-Z]{0,9})-(\d{2}[a-zA-Z]{0,9})$小数
  3. 2个字符
  4. 0-9
  5. 再次
  6. -小数,
  7. 再次
  8. 2个字符
  9. 字符串的结尾

答案 1 :(得分:1)

您可以使用此正则表达式进行匹配:

^\d{1,2}[a-zA-Z]{1,9}-\d{1,2}[a-zA-Z]{1,9}$

RegEx Demo

如果您使用的是.matches()方法,则不需要^$

  • \d{1,2}:匹配1或2位数字
  • [a-zA-Z]{1,9}:匹配1到9个英文字母
  • -:匹配文字连字符

答案 2 :(得分:0)

我想接受“ nn”并且不接受任何一个字符char,因此,对于单个序列:

[0,1]{2}\D{0,9}

说明:

[0,1]{2}->仅接受0和1作为数字正好两次;

\D{0,9}->接受0到9个通用数字。

编辑:您说过

  

其中n表示从0到1的数字

但是如果接受22may,则需要0到9之间的数字,因此必须使用\d

\d{2}\D{0,9}

答案 3 :(得分:0)

尝试一下 [0-1] {2} + [a-z] {9}。 +-