0

A string outputs below result.

test179901034102 00:00:00:00:00:01 Open
test179901083723 00:00:00:00:00:01 Open
test179901153595 00:00:00:00:00:01 Open
test179901187836 00:00:00:00:00:01 Open

 WebElement element = driver.findElement(By.xpath("//div[contains(@class,'filteredTable')]"));
 System.out.println( element.getText()); // returns the above string 

My intention is to get the values test179901034102 , test179901083723 etc as a list separately from the string.

How can i use regex to get the values(`test179901034102 , test179901083723) from the below string

test179901034102 00:00:00:00:00:01 Open
test179901083723 00:00:00:00:00:01 Open
test179901153595 00:00:00:00:00:01 Open
test179901187836 00:00:00:00:00:01 Open
2
  • 2
    Did you try anything? You really should. Commented Jul 16, 2017 at 6:04
  • iam trying to apply regex...since am new to regex it takes more time to find a valid solution for this..thats why i decided to share here.. Commented Jul 16, 2017 at 6:08

3 Answers 3

1

You can use ^[^ ]+.

Example:

String s = "test179901034102 00:00:00:00:00:01 Open\n" +
    "test179901083723 00:00:00:00:00:01 Open\n" +
    "test179901153595 00:00:00:00:00:01 Open\n" +
    "test179901187836 00:00:00:00:00:01 Open";

Pattern pattern = Pattern.compile("^[^ ]+", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
  System.out.println(matcher.group(0));
}

See: https://regex101.com/r/aZYgvC/1

Sign up to request clarification or add additional context in comments.

Comments

0

Please try this core java code and modify according to your platform

import java.util.regex.*;
public class HelloWorld{

 public static void main(String []args){
   String input="test179901034102 00:00:00:00:00:01 Open\n test179901083723 00:00:00:00:00:01 Open\n test179901153595 00:00:00:00:00:01 Open\n test179901187836 00:00:00:00:00:01 Open";

  Pattern pattern=Pattern.compile("test[0-9]*");
  Matcher m = pattern.matcher(input);

  while (m.find()) {
    System.out.println(m.group(0));
  }

  Pattern pattern2=Pattern.compile("([0-9]{2}:){5}[0-9]{2}");
  Matcher m2 = pattern2.matcher(input);

  while (m2.find()) {
   System.out.println(m2.group(0));
  }

 }
}

Use https://www.freeformatter.com/java-regex-tester.html#ad-output to try out regexes

Comments

0

XPath 1.0 does not support regular expressions. XPath 2.0 has some functions which support regular expressions: matches(), replace(), tokenize()

driver.findElement(By.xpath("//div[matches(text(),'test\d+')"));

2 Comments

but that solutions does not work because the text will be dynamic generated ones..it should not start with 'test' all times
Please add this to your question description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.