0

Question closed because I misunderstood the situation. To show my stupidity though, I'll not remove what I wrote.

I'd like to encode a piece of string into Pattern, and get the string back.

I tried:

String s = buff.readLine();
Pattern p = new Pattern(s);

and use the following to retrieve my string

System.out.println(p.toString());

But it didn't work, the output is just the "package name@(some random things)... I tried Pattern p = Pattern.compile (s); but I got an error from the compiler.

5
  • 1
    Why do you need to use Pattern?
    – Anon.
    Feb 9, 2010 at 23:38
  • @Bo: please add the exact error from toString(), and the compiler error you got for compile(s). java.util.regex.Pattern has no public constructor, so I can't see how your sample would compile, unless it was some different Pattern class... maybe check your import? Feb 9, 2010 at 23:44
  • How about System.out.println(s)? Or are you relying on Pattern to do some validation?
    – Jack Leow
    Feb 9, 2010 at 23:53
  • 1
    apologies, it's a bad question. I just realized that Pattern is a class developed by someone else, not java.util.Regex.Pattern
    – segfault
    Feb 10, 2010 at 0:14
  • you should close the question then Feb 10, 2010 at 0:16

3 Answers 3

1

Well I just tried this:

Pattern p = Pattern.compile("Hello");
System.out.println( p.toString() );

And it worked, printing out 'Hello'.

Are you importing the java.util.regex.Pattern package?

1

The javadoc for Pattern#toString() seems to indicate that the source of the complete regex is only returned since java 1.5. However, Pattern#pattern() does not have a since tag, so it is presumably available since the class was introduced (java 1.4). Try System.out.println(p.pattern());

0

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 searching other strings. It's a regular expression engine. Let me give you an example of the use of a Pattern.

We really have 2 objects when using Regular Expressions in Java. Pattern, and Matcher.

Pattern = A Regular Expression.
Matcher = All of the Matches found when we apply the Pattern to a String.

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

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);
}

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: .([1234567890]{2}:[1234567890]{2}). group 0 would be the entire match, and would result in returning the String given.

So, I hope you understand why it's extremely weird to be using a Pattern to store a String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.