Regexp Octal question

Discussion in 'Java' started by julien, Jun 25, 2007.

  1. julien

    julien Guest

    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.
     
    julien, Jun 25, 2007
    #1
    1. Advertising

  2. julien

    hiwa Guest

    On Jun 26, 6:28 am, julien <> 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
     
    hiwa, Jun 26, 2007
    #2
    1. Advertising

  3. julien

    hiwa Guest

    On Jun 26, 9:23 am, hiwa <> wrote:
    > On Jun 26, 6:28 am, julien <> 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"));
    }
    }
     
    hiwa, Jun 26, 2007
    #3
  4. 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.
     
    Daniele Futtorovic, Jun 26, 2007
    #4
  5. julien

    julien Guest

    On 26 juin, 14:17, Daniele Futtorovic <>
    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.
     
    julien, Jun 26, 2007
    #5
  6. On 26/06/2007 16:02, julien allegedly wrote:
    > On 26 juin, 14:17, Daniele Futtorovic <>
    > 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/.
     
    Daniele Futtorovic, Jun 27, 2007
    #6
  7. julien

    Roedy Green Guest

    On Mon, 25 Jun 2007 21:28:37 -0000, julien <>
    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
     
    Roedy Green, Jun 28, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kermit Lowry
    Replies:
    0
    Views:
    379
    Kermit Lowry
    Sep 25, 2003
  2. Hostos
    Replies:
    7
    Views:
    5,244
    La'ie Techie
    Oct 15, 2003
  3. Mateusz_madi

    Question about octal

    Mateusz_madi, Dec 31, 2010, in forum: C Programming
    Replies:
    13
    Views:
    638
    Hans Vlems
    Jan 4, 2011
  4. 7stud --
    Replies:
    2
    Views:
    128
    mortee
    Oct 15, 2007
  5. Joao Silva
    Replies:
    16
    Views:
    368
    7stud --
    Aug 21, 2009
Loading...

Share This Page