Regex Matching, Java. Match & Extract
NickName:Qbert Ask DateTime:2017-02-21T09:52:42

Regex Matching, Java. Match & Extract

I know this has been asked a million times, but I can't get it to work. I'm filtering strings from a game's web API (http://pathofexile.com/api/public-stash-tabs -- Careful, ~5MB of data will retrieve from GET), trying to find which type of attribute I'm looking at, because I later need to replace it. (I'm looking at the "explicitMods" array within each of the Item objects, and identifying which type of modifier it is.)

My goal is to first identify which type of modifier I am dealing with, and then use String.replaceAll to substitute the proper strings with ## so that I can later replace ## with actual values and search. I would be storing the values, or ranges, so that I can later identify what matches. String.replaceAll not included here, because that bit works just fine.

This is my test class. All tests fail. I did test each pattern on regex101.com, to be sure but they only have javascript, php, python, and golang testers. Each method comment has a link to the test I made on regex101.

package arbitrary.package.name;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.assertTrue;

public class RegexTests {

  private static Logger log = LoggerFactory.getLogger(RegexTests.class);

  @Test
  public void testIntegers() {
    // https://regex101.com/r/PVfYGX/1
    assertTrue(compileAndMatch("/.*(.\\d+).+(.\\d+).*/", "Adds 80 to 115 Physical Damage"));
  }

  @Test
  public void testIntegersWithRanges() {
    // https://regex101.com/r/u3UQqM/1
    assertTrue(compileAndMatch("/.*(\\d+-\\d+).*(\\d+-\\d+).*/", "Adds (4-5) to (8-9) Physical Damage"));
  }

  @Test
  public void testDecimals() {
    // https://regex101.com/r/CpaV1y/1
    assertTrue(compileAndMatch("/.*(\\d+.?\\d+).*/", "0.2% of Elemental Damage Leeched as Life"));
  }

  private boolean compileAndMatch(String regex, String text) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(text);
    log.info("{} {} \"{}\"", regex, matcher.matches() ? "matches" : "does not match", text);
    return pattern.matcher(text).matches();
  }
}

Sample stacktrace (all are the same):

2017-02-20 20:35:44.876 [main] INFO  arbitrary.package.name.RegexTests - /.*(\d+.?\d+).*(\d+.?\d+).*/ does not match "Adds (4-5) to (8-9) Physical Damage" 

java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:86)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at org.junit.Assert.assertTrue(Assert.java:52)
    at arbitrary.package.name.RegexTests.testIntegersWithRanges(RegexTests.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Thank you in advance for your help,

Copyright Notice:Content Author:「Qbert」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42357319/regex-matching-java-match-extract

Answers
Scary Wombat 2017-02-21T02:04:00

Use\n\n\".*(\\\\d+-\\\\d+).*(\\\\d+-\\\\d+).*\"\n\n\nJava\n\nString regex = \".*(\\\\d+-\\\\d+).*(\\\\d+-\\\\d+).*\";\nString text = \"Adds (4-5) to (8-9) Physical Damage\";\nPattern pattern = Pattern.compile(regex);\nMatcher matcher = pattern.matcher(text);\n\nSystem.out.println(matcher.matches());\n",


More about “Regex Matching, Java. Match & Extract” related questions

Regex Matching shortest match instead of longest

I am trying to write regex but having trouble making it work for some cases. Here is the regex (\/[ABCGIKLNPRSUV])?(\/RC-[A-Z0-9]{2,6})? The matches could be one of the following /R/RC-ABC123 /R...

Show Detail

Regex Match - outputing any matching chars in a file

I am trying to use the Regex.Match method to find matching characters in a file. At the moment the regex match method uses the line of the file in memory (strLine) and checks it against the specifi...

Show Detail

Regex Matching, Java. Match & Extract

I know this has been asked a million times, but I can't get it to work. I'm filtering strings from a game's web API (http://pathofexile.com/api/public-stash-tabs -- Careful, ~5MB of data will retri...

Show Detail

Regex not matching

I expect regex "[a-zA-Z]\\d{6}" to match "z999999" but it is not matching as an empty List is mapped : val lines = List("z999999"); //> lines : List[String] = List(z99999...

Show Detail

Incremental regex matching

Problem: The program needs to read from an input stream of characters, and match the input against a predefined list of regex patterns. The program should report a match as soon as it is found (rep...

Show Detail

Regex match expected but not matching

Given this string of ; delimited values a;; z toy;d;hh toy ;b;;jj z; d;23 d;23td ;;io; b y;b;12 z a;b;bb;;;34 z and this regex ^(?!([^\r\n;]*);(?:(b|d))(?:;|$)).*\R I am look

Show Detail

regex match two words based on a matching substring

there are 4 strings as shown below ABC_FIXED_20220720_VALUEABC.csv ABC_FIXED_20220720_VALUEABCQUERY_answer.csv ABC_FIXED_20220720_VALUEDEF.csv ABC_FIXED_20220720_VALUEDEFQUERY_answer.csv Two stri...

Show Detail

Regex - Match parentheses without matching their contents

I just need to match parentheses around some content that has to match specific criteria. I need to match only the parentheses so that I can then do a quick replacement of only those parentheses an...

Show Detail

Why unicode word is not matching but english word will match with my regex

Hi i'm trying to match the any give word if if it begins with (same letters) exact match (exact word) My regex is something like this /(?<=[(["]\s*|\b)(टीम\w*|UAE\w*|afri\w*)(?=\b|\s*[\])...

Show Detail

Regex for matches but does not match?

How can I get a regex to match anything matching [2-9][0-9][0-9] but not matching 303|719|720|970 For example, the regex should match 406, but not 719 (as it matches the second regex) or 3333 (...

Show Detail