Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regexp Octal question

Reply
Thread Tools

Regexp Octal question

 
 
julien
Guest
Posts: n/a
 
      06-25-2007
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.

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      06-26-2007
On Jun 26, 6:28 am, julien <(E-Mail Removed)> wrote:
> 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.


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

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      06-26-2007
On Jun 26, 9:23 am, hiwa <(E-Mail Removed)> wrote:
> On Jun 26, 6:28 am, julien <(E-Mail Removed)> wrote:
>
>
>
> > 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.

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

 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      06-26-2007
On 25/06/2007 23:28, julien allegedly wrote:
> 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.
>


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.
 
Reply With Quote
 
julien
Guest
Posts: n/a
 
      06-26-2007
On 26 juin, 14:17, Daniele Futtorovic <(E-Mail Removed)>
wrote:
> On 25/06/2007 23:28, julien allegedly wrote:
>
>
>
> > 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.

>
> 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.


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.

 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      06-27-2007
On 26/06/2007 16:02, julien allegedly wrote:
> On 26 juin, 14:17, Daniele Futtorovic <(E-Mail Removed)>
> wrote:
>> On 25/06/2007 23:28, julien allegedly wrote:
>>
>>
>>
>>> 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.

>> 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.

>
> 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.
>


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/.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-28-2007
On Mon, 25 Jun 2007 21:28:37 -0000, julien <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>
>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about octal Mateusz_madi C Programming 13 01-04-2011 07:50 AM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
question about some octal formatted output? 7stud -- Ruby 2 10-14-2007 11:23 PM
converting characters to octal Hostos Java 7 10-15-2003 06:07 AM
strange replacement of . and , with octal counterpart Kermit Lowry Perl 0 09-25-2003 11:04 AM



Advertisments