6 Replies Latest reply: Apr 8, 2009 11:38 AM by 807588 RSS

    Regex - matching literal characters

    807588
      Im trying to match the following pattern using regex:

      The string begins with a literal '\' is followed by any number of letters and/or numbers and ends with '&0]'

      e.g. '\07761739009B&0]'

      Im trying to devise my pattern but Im not exactly sure how to work with matching literal characters, I was lead to believe a '//' would dictate that the character is literal but this doesnt work:
      Pattern Serial = Pattern.compile("(\\/.*+\\&0])");
      Thanks in advance for any advice
        • 1. Re: Regex - matching literal characters
          800308
          \ is special to Java and regex's... hence \\\\ is required to match a literal backslash... I just love manipulating UNC paths in regex in java, especially when I'm generating the java code in a bash script.
          package forums;
          
          class BackslashHeaven
          {
            public static void main(String[] args) {
              try {
                System.out.println("C:\\Java\\home\\src\\forums\\BackslashHeaven.java".replaceAll("\\\\","/"));
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
          • 2. Re: Regex - matching literal characters
            796440
            \ is an escape character both in Java string literals and in regex.
            "\\"
            produces a String containing a single \ character. But for a literal \, regex needs \\. So
            "\\\\"
            produces a single string containing
            \\
            which in regex becomes a single literal \.

            Also, I don't think you need to escape &. And you might need to escape ] but I'm not sure--it might be okay bare if there was no preceding [.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
            • 3. Re: Regex - matching literal characters
              807588
              Hey Racha, please don't cross-post the same question on multiple fora without notifying us that you're doing this and without providing links. It's not fair to us to ask us to possibly duplicate work that has been solved elsewhere as we're all volunteers with lives and families of our own. If you do this again, you will risk losing many potential helpers from all fora. Seriously.
              • 4. Re: Regex - matching literal characters
                807588
                Really sorry about that, Im pretty new to Java forums and didnt realise it would be a problem. I will stick to posting on just one in future. I appreciate the warning. Thanks, Rach
                • 5. Re: Regex - matching literal characters
                  796440
                  racha0601 wrote:
                  Really sorry about that, Im pretty new to Java forums and didnt realise it would be a problem. I will stick to posting on just one in future. I appreciate the warning. Thanks, Rach
                  We appreciate that, but (sorry to nitpick), it has nothing at all to do with Java. It's really just common sense if you stop to think about it.

                  Anyway, no harm done. Just keep it in mind for future posts.
                  • 6. Re: Regex - matching literal characters
                    807588
                    racha0601 wrote:
                    Really sorry about that, Im pretty new to Java forums and didnt realise it would be a problem. I will stick to posting on just one in future. I appreciate the warning. Thanks, Rach
                    but please reply to all posts in all fora.