Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're using a regex Pattern object to store and retrieve a String. This makes no sense. A Pattern is not used for storing Strings. A Pattern is used for <em>searching</em> other strings. It's a regular expression engine. Let me give you an example of the use of a Pattern.</p> <p>We really have 2 objects when using Regular Expressions in Java. Pattern, and Matcher.</p> <p>Pattern = A Regular Expression.<br> Matcher = All of the Matches found when we apply the Pattern to a String.</p> <p>Let me give you an example of Pattern and Matcher, we'll search for four digits, separated by a colon, like as in time, ie 12:42</p> <pre><code>long timeL; Pattern pattern = Pattern.compile(".*([1234567890]{2}:[1234567890]{2}).*"); Matcher matcher = pattern.matcher("Match me! 12:42 Match me!"); if (matcher.matches()) { String timeStr = matcher.group(1); System.out.println("Just the time: "+timeStr); System.out.println("The entire String: "+matcher.group(0)); String[] timeParts = timeStr.split("[:]"); int hours = Integer.parseInt(timeParts[0]); int minutes = Integer.parseInt(timeParts[1]); timeL = (hours*60*60*1000) + (minutes*60*1000); System.out.println(timeL); } </code></pre> <p>After we've applied the Pattern to the String, and gotten a Matcher, we ask if the Matcher actually has a Match or not. You'll notice that we then request group 1, which is the match in the parantheses in: .<em>([1234567890]{2}:[1234567890]{2}).</em> group 0 would be the entire match, and would result in returning the String given.</p> <p>So, I hope you understand why it's extremely weird to be using a Pattern to store a String.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload