Java regex simple match and replace

So, I have myString , which contains the string

 "border-bottom: solid 1px #ccc;width:8px;background:#bc0b43;float:left;height:12px" 

I want to use a regular expression to verify that it contains "width:8px" (\bwidth\s*:\s*(\d+)px) \ s *: \ s * (\ d +) px)

If the value is true, add value width (i.e., for Example 8 above) in myList.

Attempt:

 if (myString.contains("\\bwidth\\s*:\\s*(\\d+)px")) { myList.add(valueofwidth) //unsure how to select the width value using regex } s *: \\ s * (\\ d +) px")) { if (myString.contains("\\bwidth\\s*:\\s*(\\d+)px")) { myList.add(valueofwidth) //unsure how to select the width value using regex } select the width value using regex if (myString.contains("\\bwidth\\s*:\\s*(\\d+)px")) { myList.add(valueofwidth) //unsure how to select the width value using regex } 

Any help?

EDIT: So I looked at the method contains , and found that it does not allow regular expression. matches will permit regular expression, but it is looking for a match.

+4
source share
3 answers

To do this, you need to use Matcher#find() .

From the documentation: -

Attempts to find the next subsequence of the input sequence that matches the pattern.

And then you can get captured by a group of: -

 Matcher matcher = Pattern.compile("\\bwidth\\s*:\\s*(\\d+)px").matcher(myString); if (matcher.find()) { myList.add(matcher.group(1)); } \\ s *: \\ s * (\\ d +) px") matcher (myString);. Matcher matcher = Pattern.compile("\\bwidth\\s*:\\s*(\\d+)px").matcher(myString); if (matcher.find()) { myList.add(matcher.group(1)); } 
+5
source

You must use the Matcher and matcher.find ():

 Pattern pattern = Pattern.compile("(\\bwidth\\s*:\\s*(?<width>\\d+)px)"); Matcher matcher = pattern.matcher(args); while (matcher.find()) { myList.add(matcher.group("width"); } bwidth \\ s *: \\ s * (<width> \\ d +) px)?"); Pattern pattern = Pattern.compile("(\\bwidth\\s*:\\s*(?<width>\\d+)px)"); Matcher matcher = pattern.matcher(args); while (matcher.find()) { myList.add(matcher.group("width"); } ; Pattern pattern = Pattern.compile("(\\bwidth\\s*:\\s*(?<width>\\d+)px)"); Matcher matcher = pattern.matcher(args); while (matcher.find()) { myList.add(matcher.group("width"); } 
+4
source

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.

+2
source

All Articles