如何使用REGEX在第一个点之前找到带有空格字符的多个点

时间:2019-01-08 06:55:49

标签: regex

#^.[\S]+\.[\S]+\.(.*)$

我已使用此正则表达式查找多个点,但如果我的字符串在第一个点之前包含空格,则它不起作用

^.[\S]+\.[\S]+\.(.*)$
  

我希望正则表达式可以找到该值

adajda9a b0a09.haa.ajada

teast.php.tasd

madnadak.ajada.a.jjhjhh

adjahdja.dfajha.ada.adjahdaj..jajjjjjhjha....dahhhhhbbja...

madkaja.adhakjda.sjjj

sadada.asdaa.jadfajk jadajda ajdhajda  ada- 0(i09d0a9 )_) aciai

aadhadka.adad.akdjajdka0sd009999a.o999

adajda9a b0a09.haa.ajada

enter image description here

3 个答案:

答案 0 :(得分:2)

如果您只想匹配至少两个点的字符串,那么为什么不使用它:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>           
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>            
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>            
        <scope>test</scope>
    </dependency>        
    <dependency>
        <groupId>it.ozimov</groupId>
        <artifactId>spring-boot-email-core</artifactId>
        <version>0.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>2.3.0</version>
    </dependency>
</dependencies>

Demo

您也可以使用前瞻性代码来编写此代码:

^.*\..*\..*$

答案 1 :(得分:1)

我创建了一个正则表达式,它可以匹配其中包含多个点并且在点出现之前只有一个空格的字符串。

^[^.\s]* [^\s]*(?:\..*\..*)+$

演示: https://regex101.com/r/UQksQK/4/

如果要在点前留几个空格,请使用

^[^\.\s]* +.*(?:\..*\..*)+$

这也将匹配:

adajda9a     b0a09.haa.ajada.123

如果要禁止点之间的空格字符,请将正则表达式更改为:

^[^.\s]* +[^\s]*(?:\.[^\s]*\.[^\s]*)+$

它将不匹配字符串(点之间有空格):

adajda9a b0a09.ha a.ajada.123

答案 2 :(得分:0)

每个注释以使行与第一个多个点之前的空格匹配:

^[^\.]* .*\..*\..*$

测试:

$ cat test.regexp
teast.php.tasd
madnadak.ajada.a.jjhjhh
adjahdja.dfajha.ada.adjahdaj..jajjjjjhjha....dahhhhhbbja...
madkaja.adhakjda.sjjj
sadada.asdaa.jadfajk jadajda ajdhajda  ada- 0(i09d0a9 )_) aciai
aadhadka.adad.akdjajdka0sd009999a.o999
adajda9a b0a09.haa.ajada

$ egrep "^[^\.]* .*\..*\..*$" test.regexp
adajda9a b0a09.haa.ajada