RegEx with pattern and matcher in JAVA -
I have a text / html file, I need some information from this For this, I am using RegEx as the code given below.
My problem is, I want the results of pattern P and pattern L in the same matching because the result is very important in the system in my code. The wrong sequence of output is because it prints the results of the pattern P and then the result of the pattern.
How to solve this problem?
string pattern 1 = "& lt; img class = \" gallery element shown \ "data-src = \" "; string pattern 2 =" \ "src = \" \ " /> "; String pattern 3 = "& lt; img class = \" galleryElement shown \ "src = \" "; string pattern 4 =" \ "/>; Pattern p = Pattern.compile (Pattern.quote (pattern1) + "(. *?)" + Pattern.quote (pattern2)); Pattern l = Pattern.compile (Pattern.quote (pattern3) + "(. *?)" + Pattern.quote (pattern4)); Matcher m = p.matcher (res.toString ()); While (m.find ()) {System.out.println (m.group (1)); } Matcher n = l.matcher (res.toString ()); While (n.find ()) {System.out.println (n.group (1)); }
Because you have not defined rules, this is just a try, I know That you need any modification in the pattern:
& lt; Img class = "gallery element shown" (data -)? Src = "([^"] *?) "
Pattern Details: (data -)? Src =" ([^ "] * ?) ".
(to group and capture \ 1 (optional): data- 'data-')? At the end of \ 1 "=" 'src = "' (group and capture to \ 2: [^"] *? Except for any character: '' '(0 or more often (at least))' Sample code: string pattern = "& lt; img class = \" gallery element shown \ "(data -)? Src = \ "([^ \"] *?) \ ""; Pattern p = pattern.compile (pattern, pattern csexxntyvie); michter m = p. Metcharch ("& lt; img class = \" gallery element shown \ " Data-SRC = \ "abc \" />;); while (m.find ()) {System.out.println (m.group (2))}}
Comments
Post a Comment