在正则表达式中仅返回具有OR条件的一组

时间:2018-07-26 15:29:22

标签: regex regex-negation regex-lookarounds regex-group regex-greedy

我必须编写一个正则表达式才能从句子中获取电子邮件地址。我希望仅将其与组1一起退回。

正则表达式:

D().run {
  C().run {
    foo() // accessible
  }
}

输入字符串:

public class Pawan {

    public static WebDriver driver;

    public static void main(String[] args){

    System.setProperty("webdriver.firefox.marionette","C:\\Users\\Lalit-pc\\Desktop\\geckodriver-v0.21.0-win64\\geckodriver.exe");

    driver = new FirefoxDriver();

    }

     @Test
     public void test()  {
         WebDriver driver = new FirefoxDriver();    
      driver.manage().window().maximize();
      driver.get("https://www.google.com/");
      driver.findElement(By.linkText("Find a Physician")).click();
      driver.findElement(By.id("searchZip")).sendKeys("32806");   
      driver.findElement(By.linkText("Mile Radius")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }

      driver.findElement(By.linkText("20")).click(); 
      driver.findElement(By.linkText("Specialty")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }
      driver.findElement(By.linkText("Anesthesiology")).click();
      driver.findElement(By.cssSelector("input[type='submit'][value='Search Now']")).click();
      String str= driver.findElement(By.xpath(".//*[@id='finderListView']/div[3]/div[1]/div/div[1]/p")).getText();

      if("Anesthesiology".equals(str))

          System.out.println("Physician Search Successful");
      else
          System.out.println("Physician Search NOT Successful");

      driver.findElement(By.linkText("Browse Locations")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }
      driver.findElement(By.xpath(".//*[@id='sidebarMenu']/div[1]/form/div/input")).sendKeys("32806");
      driver.findElement(By.xpath(".//*[@id='sidebarMenu']/div[1]/form/input")).click();

        try{
               Thread.sleep(6000);
              }catch (InterruptedException ie1) {
                ie1.printStackTrace();
              } 
      driver.findElement(By.xpath(".//*[@id='sidebarMenu']/div[2]/section/div/ul/li[1]/a")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }   
      WebElement divElement = driver.findElement(By.xpath(".//*[@id='overflow-autoScroll']/li[1]/ul/li/a/div[2]/span[3]"));
      String stri = divElement.getText();
      if(stri.contains("32806"))

          System.out.println("Location Search successful");
      else
          System.out.println("Location Search not successful");

      driver.findElement(By.xpath("html/body/header/div[1]/div[2]/a[3]")).click();   

      driver.findElement(By.linkText("Health Topics")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }

      driver.findElement(By.linkText("Diabetes")).click();
      WebElement divElementtwo = driver.findElement(By.xpath("html/body/div[4]/div/div[1]/div[1]/h2"));
      String strn = divElementtwo.getText();
              if(strn.contains("Diabetes"))        
          System.out.println("Blog Search successful");
      else
          System.out.println("Blog Search not successful");



      }
}

我希望是否有任何匹配的字符串,然后将其返回到Group1中。

有什么办法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式:

(?=\S+@)([^<\s]+@.*(?=[>\]]))
  • (?=\S+@)正向前进,断言其后是任何非空白字符,后跟@
  • ([^<\s]+@.*(?=[>\]]))捕获组。捕获所有非空格,非^字符,后跟@,以及不超过]>字符的所有字符。

您可以测试正则表达式here