The main problem is that the contains() does not accept a regular expression, it takes a literal String.
matches() , on the other hand, takes a regex option, but must match the entire string to return true.
Next, once you get your line, you can use replaceAll() to extract the contents of the target:
if (myString.matches(".*\\bwidth\\s*:\\s*\\d+px.*")) { myList.add(myString.replaceAll(".*\\bwidth\\s*:\\s*(\\d+)px.*", "$1")) } \\ s *:. \\ s * \\ d + px *.")) { if (myString.matches(".*\\bwidth\\s*:\\s*\\d+px.*")) { myList.add(myString.replaceAll(".*\\bwidth\\s*:\\s*(\\d+)px.*", "$1")) } \\ s *:. \\ s * (\\ d +) px *.", "$ if (myString.matches(".*\\bwidth\\s*:\\s*\\d+px.*")) { myList.add(myString.replaceAll(".*\\bwidth\\s*:\\s*(\\d+)px.*", "$1")) }
It replaces the entire contents of the input string String Group # 1, which fixes your original regex.
Please note that I have removed redundant brackets from the original source of the regular expression, but left them to replace, to capture the targeted content.
source share