JPad
Snippets
Download
Help
Contact
36573959-example
Example code for SO question 36573959.
Code
public static void main(String args[]) { String testCases[] = new String[] { "<font color=\"black\">", "BlaBla <font color=\"red\">", "<font size=\"2\" color=\"white\">", "<font size=\"2\">", "<font size=\"10\"><font color=\"black\"><font size=\"10\">" }; for (String testCase : testCases) { System.out.print(testCase + " = "); matchFontColor(testCase); } } public static void matchFontColor(String html) { Pattern p = Pattern.compile("<font[^>]*color=\"[^\"]+\"[^>]*>"); Matcher m = p.matcher(html); if (m.find()) { System.out.println(m.group()); } else { System.out.println("no match"); } }
Result
Console
<font color="black"> = <font color="black"> BlaBla <font color="red"> = <font color="red"> <font size="2" color="white"> = <font size="2" color="white"> <font size="2"> = no match <font size="10"><font color="black"><font size="10"> = <font color="black">
Bytecode
Compiled from "RunnContainer.java" public class io.jpad.scratch.RunnContainer { public io.jpad.scratch.RunnContainer(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_5 1: anewarray #2 // class java/lang/String 4: dup 5: iconst_0 6: ldc #3 // String <font color=\"black\"> 8: aastore 9: dup 10: iconst_1 11: ldc #4 // String BlaBla <font color=\"red\"> 13: aastore 14: dup 15: iconst_2 16: ldc #5 // String <font size=\"2\" color=\"white\"> 18: aastore 19: dup 20: iconst_3 21: ldc #6 // String <font size=\"2\"> 23: aastore 24: dup 25: iconst_4 26: ldc #7 // String <font size=\"10\"><font color=\"black\"><font size=\"10\"> 28: aastore 29: astore_1 30: aload_1 31: astore_2 32: aload_2 33: arraylength 34: istore_3 35: iconst_0 36: istore 4 38: iload 4 40: iload_3 41: if_icmpge 87 44: aload_2 45: iload 4 47: aaload 48: astore 5 50: getstatic #8 // Field java/lang/System.out:Ljava/io/PrintStream; 53: new #9 // class java/lang/StringBuilder 56: dup 57: invokespecial #10 // Method java/lang/StringBuilder."<init>":()V 60: aload 5 62: invokevirtual #11 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 65: ldc #12 // String = 67: invokevirtual #11 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 70: invokevirtual #13 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 73: invokevirtual #14 // Method java/io/PrintStream.print:(Ljava/lang/String;)V 76: aload 5 78: invokestatic #15 // Method matchFontColor:(Ljava/lang/String;)V 81: iinc 4, 1 84: goto 38 87: return public static void matchFontColor(java.lang.String); Code: 0: ldc #16 // String <font[^>]*color=\"[^\"]+\"[^>]*> 2: invokestatic #17 // Method java/util/regex/Pattern.compile:(Ljava/lang/String;)Ljava/util/regex/Pattern; 5: astore_1 6: aload_1 7: aload_0 8: invokevirtual #18 // Method java/util/regex/Pattern.matcher:(Ljava/lang/CharSequence;)Ljava/util/regex/Matcher; 11: astore_2 12: aload_2 13: invokevirtual #19 // Method java/util/regex/Matcher.find:()Z 16: ifeq 32 19: getstatic #8 // Field java/lang/System.out:Ljava/io/PrintStream; 22: aload_2 23: invokevirtual #20 // Method java/util/regex/Matcher.group:()Ljava/lang/String; 26: invokevirtual #21 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 29: goto 40 32: getstatic #8 // Field java/lang/System.out:Ljava/io/PrintStream; 35: ldc #22 // String no match 37: invokevirtual #21 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 40: return }
Jpad Version:1.05
viewCount:2921
Creator:
mellamokb
Latest Editor:
mellamokb
Tags
java
stackoverflow
36573959
Download
Copy As Markdown:
**code [36573959-example.java](/example/1u/36573959-example)** public static void main(String args[]) { String testCases[] = new String[] { "
", "BlaBla
", "
", "
", "
" }; for (String testCase : testCases) { System.out.print(testCase + " = "); matchFontColor(testCase); } } public static void matchFontColor(String html) { Pattern p = Pattern.compile("
]*color=\"[^\"]+\"[^>]*>"); Matcher m = p.matcher(html); if (m.find()) { System.out.println(m.group()); } else { System.out.println("no match"); } } **Result:** <font color="black"> = <font color="black"> BlaBla <font color="red"> = <font color="red"> <font size="2" color="white"> = <font size="2" color="white"> <font size="2"> = no match <font size="10"><font color="black"><font size="10"> = <font color="black">