Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Regexp Octal question

Thread view: 
julien - 25 Jun 2007 22:28 GMT
Hi,

Strange question, if somebody have an answer, will be really helpfull.

I used a regexp (octal) :  < \0 > to find < (char)0 > char in a
string.

1) If i get the regexp \0 from a text file, the regexp NOT match.
2) If i defines directly the regexp \0 in java code, the regexp MATCH.
3) If i get regexp \x00 (another way to match (char)0) from a file,
the regexp MATCH
4) If i defines directly the regexp \x00 in java code, the regexp
MATCH

So, to resume, i cant successfull get the regexp from a file.
Any idea ? This case is really strange for me.
Thanks in advance.
hiwa - 26 Jun 2007 01:23 GMT
> Hi,
>
[quoted text clipped - 13 lines]
> Any idea ? This case is really strange for me.
> Thanks in advance.

I don't understand your question.
Post a small demo code that is generally compilable, runnable and
could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html
and http://www.yoda.arachsys.com/java/newsgroups.html
hiwa - 26 Jun 2007 01:39 GMT
> > Hi,
>
[quoted text clipped - 18 lines]
> could reproduce your problem. See:http://homepage1.nifty.com/algafield/sscce.html
> andhttp://www.yoda.arachsys.com/java/newsgroups.html

And there should be no problem. Try this code:
/*** content of the regex.txt file *************************
\0103
************************************************************/
import java.io.*;

public class Julien{

 public static void main(String[] args) throws Exception{
   String regexFromFile;
   String regexFromCode = "\\0103";
   String text = "ABCDEF";

   BufferedReader br = new BufferedReader(new
FileReader("regex.txt"));

   regexFromFile = br.readLine();

   System.out.println(text.replaceAll(regexFromFile, "G"));
   System.out.println(text.replaceAll(regexFromCode, "G"));
 }
}
Daniele Futtorovic - 26 Jun 2007 13:17 GMT
> Hi,
>
[quoted text clipped - 13 lines]
> Any idea ? This case is really strange for me.
> Thanks in advance.

What mechanism are you using to read the file's content? Maybe it
interpretes the \0 as an END_OF_STRING marker.

BTW, if what you've described above is all your regex does, you might as
well go with String#indexOf(char).

Regards,
 df.
julien - 26 Jun 2007 15:02 GMT
On 26 juin, 14:17, Daniele Futtorovic <da.futt.n...@laposte.net>
wrote:

> > Hi,
>
[quoted text clipped - 22 lines]
> Regards,
>   df.

Your right. I have made some test.
Then i get \0 from file, is not get like the special char \0 but like
"\" + "0".
If i put \00 instead of \0 in the file, the regexp works (\0 = \00)
So, i have a file with regexp \0, instead of change the file, i test
and replace all \0 found in the file with \00.
Daniele Futtorovic - 27 Jun 2007 18:33 GMT
> On 26 juin, 14:17, Daniele Futtorovic <da.futt.n...@laposte.net>
> wrote:
[quoted text clipped - 27 lines]
> So, i have a file with regexp \0, instead of change the file, i test
> and replace all \0 found in the file with \00.

Come to think of it, I don't believe I'm right.
You didn't answer my question as to how you read the file's content, but
I cannot imagine any text reader actually thus interpreting the two
chars "\" and "0".
No, what's much more likely the explanation, IMO, is that the octal
notation is defined, roughly, as "any number, preceded by '\0'". In your
case, the number would be "". But that's NaN.
So, I think your assumption that "\0", as a regexp, ought to match the
NULL char is wrong. It'd match the char "0" (needlessly quoted), or it
may be interpreted as a backreference (not too sure), but it wouldn't
match the NULL character, and it seems to me as though this might be
called "working as designed" (and documented).

You said:
> 2) If i defines directly the regexp \0 in java code, the regexp MATCH.

Yes, that is, if you define the regex using "\0", as opposed to "\\0".
The "\0" gets interpreted by -- I don't know if its the compiler, just
like "\\" gets interpreted to "\". So in the end (the runtime), in your
regex pattern, there /is/ the character NULL, not a backslash and a
zero. But when you load the pattern String from a file, it transcedes
into the regex pattern /as is/.
Roedy Green - 28 Jun 2007 15:16 GMT
>I used a regexp (octal) :  < \0 > to find < (char)0 > char in a
>string.

I have  never heard of octal in regexes. I have only seen them in
Java source literals.   Try hex. Rarely used features are likely to be
buggy.

see http://mindprod.com/jgloss/regex.html#AWKWARD
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.