2

I am using Java and have a String, sum, whose content will be something like this:

A bunch of miscellaneous text
A bunch more miscellaneous text
Handicap Accessible *value*
More miscellaneous Text
Even more miscellaneous text 

value can be Yes, No or None

I am trying to get the value of value with a regular expression. I can't just do a sum.replaceAll("^.*Handicap Accessible ","") because there are new lines and other characters which don't count in ".".

I am trying to use a regular expression but I can't get it right. Below is what I have tried, both with and without the backslashes. And notice that this is from java so I need to use two backslashes (\\):

    Pattern pat = Pattern.compile("Handicap Accessible \\([A-Za-z]*\\)");
    Matcher match = pat.matcher(sum);
    String newAccess = null;
    while (match.matches()) {
        newAccess = match.group(1);
        break;
    }

But when I print the value of newAccess it is always null. If I initialize newAccess to something else like "GLUB" then "GLUB" is what is printed at the end, meaning the matches loop is not being entered.

Any suggestions for the correct pattern to use?

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
Tony
  • 1,127
  • 1
  • 18
  • 29
  • 1
    Does your string actually have newlines in it? – Tim Biegeleisen Jun 15 '16 at 01:47
  • 1
    Does your value have backslashes around it? – OneCricketeer Jun 15 '16 at 01:48
  • 1
    What's wrong with `Handicap Accessible (Yes|None|No)`? – OneCricketeer Jun 15 '16 at 01:52
  • yes (potentially, that is sometimes) to new lines. No to backslashes (Values are Yes, No, None). cricket_007 that would find the entire string but how would I extract it? And in the case when there is an error, the value, though it should not be, may be something else (again it shouldn't be but in an eror condition) – Tony Jun 15 '16 at 01:55
  • The parenthesis in the @cricket_007 solution indicates a group so you can extract it from `matches.group(1)` The error values wouldn't be matched at all. Isn't that what you want? – Jorge Campos Jun 15 '16 at 01:57
  • I will have an expected value: No, Yes or None, depending on the test and I have to verify that the value is as expected. I could say sum.contains("Handicap Accessible No") for instance. That would work – Tony Jun 15 '16 at 01:59
  • Okay, so the regex you have in the question is a literal backslash, followed by a group of zero or more characters, then another backslash. That is why I asked. – OneCricketeer Jun 15 '16 at 02:35
  • I always forget whether in a group (for a matching group) you use ( or \( ;-) – Tony Jun 15 '16 at 12:31

3 Answers3

1

I would go for (took the input from @Tim's answer, thanks Tim):

String input = "A bunch of miscellaneous text\n" +
           "A bunch more miscellaneous text\n" +
           "Handicap Accessible None\n" +
           "Handicap Accessible Yes\n" +
           "More miscellaneous Text\n" +
           "Handicap Accessible No\n" +
           "Handicap Accessible somevalue\n" +
           "Even more miscellaneous text\n";
Pattern p = Pattern.compile("^Handicap Accessible (Yes|None|No)$", Pattern.MULTILINE);
Matcher m = p.matcher(input);
while ( m.find() ){
    System.out.println( "Value is: " + m.group(1) );
}

Following the @Qix suggestion I changed from Handicap Accessible (Yes|None|No) to "^Handicap Accessible (Yes|None|No)$", Pattern.MULTILINE it works better in a multiline text (instead of just \n)

I will output:

Value is: None
Value is: Yes
Value is: No
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
0

Replace the match.matches() with match.find():

Pattern pat = Pattern.compile("Handicap Accessible ([A-Za-z]*)");
Matcher match = pat.matcher(sum);
String newAccess = null;
while (match.find()) {
    newAccess = match.group(1);
    break;
}

the difference between of them can be seen from Difference between matches() and find() in Java Regex. Meanwhile, "\\(" means your pattern need matches the character "(".

Community
  • 1
  • 1
Heng
  • 96
  • 1
  • 5
  • Thanks. I know usually \ escapes a character, and I could never remember if you are supposed to use ( or \( for a group. Sounds like no \. I am going to try the suggestions above and let you guys know, – Tony Jun 15 '16 at 11:58
  • This worked when I used Pattern.MULTILINE and find ;-) – Tony Jun 15 '16 at 12:28
0

Well, I think this is straightforward. Just apply the regex and print what is in the capture group:

    String input = "A bunch of miscellaneous text\n" +
                   "A bunch more miscellaneous text\n" +
                   "Handicap Accessible None\n" +
                   "More miscellaneous Text\n" +
                   "Even more miscellaneous text\n";

    String regex="Handicap Accessible\\s+(Yes|No|None)";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);   
    String value=null;
    if (matcher.find()) {
        value=matcher.group(1);
    } else {
        throw new RuntimeException ("your regex is wrong dude!");
    }
    System.out.println(value);
Bruno Negrão Zica
  • 764
  • 2
  • 7
  • 16
  • This works too, but I also want it to print the bad value. Another thing I was toying with was WebElement ele = driver.findElement(By.xpath("//*[contains(text(), 'Handicap Accessible')]"); and then get the value of the text, and then it would be easier to manipulate Handicap Accessible, instead of getting the entire pageText(); – Tony Jun 15 '16 at 12:30