1 2 3 Previous Next 35 Replies Latest reply: Sep 21, 2007 6:38 AM by 800282 RSS

    Regex Issue

    807605
      In character classes ,
      [1|2|3|4]
      This regular expression matches 1 or 2 or 3 or 4 but also matches | operator
      why so ??
      I need to match only char inside character class and not | operator
      Suggest solution
        • 1. Re: Regex Issue
          807605
          [1234] or [1-4]
          • 2. Re: Regex Issue
            800282
            Because | is not a meta character inside a character class. So you should do one of the following:
            "[1-4]"
            "[1234]"
            "(1|2|3|4)" // | is the OR meta character only outside a character class
            Of which I prefer the first one.
            • 3. Re: Regex Issue
              807605
              martin@work wrote:
              [1234] or [1-4]
              this is just example i gave u
              if i want only chars like % or # or @ or !
              then [% | # | ! | ! ]
              • 4. Re: Regex Issue
                800282
                this is just example i gave u
                if i want only chars like % or # or @ or !
                then [% | # | ! | ! ]
                Then you do:
                "[%#@!]" // matches either "%". "#", "@" or "!" (matches only one character!)
                • 5. Re: Regex Issue
                  807605
                  Do not use the | character in square brackets, just put the chars you want to be matched there (and remember about escape sequences). The default behavior of [] operator indicates that it treats characters or groups of characters in logical or form.

                  HTH
                  • 6. Re: Regex Issue
                    800282
                    Do not use the | character in square brackets, just put the chars you want to be matched there
                    Yes, but the question of the OP was, why? The answer to that has been given already.

                    (and remember about escape sequences).
                    What do you mean by that?
                    • 7. Re: Regex Issue
                      807605
                      Yes , u r right
                      Actually I have a map whose keys specifies Japaneses digits and values are English digits .
                      So i have to read that map and pick up those pairs from it
                      Since map uses hashing concept it is not sure that it will give me 0 = 0 pairs first
                      and 9= 9 pairs at last
                      So i can not specify [0-9] range in my regex.
                      So i will be constructing dynamic regex like [3|4|5|1] as such
                      • 8. Re: Regex Issue
                        800282
                        ...
                        So i will be constructing dynamic regex like [3|4|5|1] as such
                        No! Again: the | (pipe) character has no special meaning inside a character class. So, *a* does NOT mean either a or b. It means a or b or |. If you want to match a or b, you should do:
                        "[ab]" // matches a or b
                        "a|b" // also: matches a or b
                        • 9. Re: Regex Issue
                          791266
                          AmitChalwade123456 wrote:
                          Yes , u r right
                          Actually I have a map whose keys specifies Japaneses digits and values are English digits .
                          So i have to read that map and pick up those pairs from it
                          Since map uses hashing concept it is not sure that it will give me 0 = 0 pairs first
                          and 9= 9 pairs at last
                          So i can not specify [0-9] range in my regex.
                          So i will be constructing dynamic regex like [3|4|5|1] as such
                          What? That didn't make sense at all.
                          • 10. Re: Regex Issue
                            807605
                            prometheuzz wrote:
                            ...
                            So i will be constructing dynamic regex like [3|4|5|1] as such
                            No! Again: the | (pipe) character has no special meaning inside a character class. So, *a* does NOT mean either a or b. It means a or b or |. If you want to match a or b, you should do:
                            "[ab]" // matches a or b
                            "a|b" // also: matches a or b
                            Yes that is applicable only in case of char but not in digits if in character class [12345] then it tries to find 12345 and not 1 or 2 or 3 or 4 or 5
                            • 11. Re: Regex Issue
                              807605
                              Yes that is applicable only in case of char but not in digits if in character class [12345] then it tries to find 12345 and not 1 or 2 or 3 or 4 or 5
                              Are u sure????
                              • 12. Re: Regex Issue
                                807605
                                SidGate wrote:
                                Yes that is applicable only in case of char but not in digits if in character class [12345] then it tries to find 12345 and not 1 or 2 or 3 or 4 or 5
                                Are u sure????
                                Try it
                                • 13. Re: Regex Issue
                                  807605
                                  I tried it... It is matching individual digts. What abt others?
                                  • 14. Re: Regex Issue
                                    800282
                                    ... character class [12345] then it tries to find 12345 and not 1 or 2 or 3 or 4 or 5
                                    Wrong!
                                    The character class *[12345]* matches one of the following characters: either *1 or 2 or 3 or 4 or 5*.
                                            for(int i = 0; i <= 10; i++) {
                                                System.out.println(i+" -> "+String.valueOf(i).matches("[12345]"));
                                            }
                                            
                                            /*
                                            output:
                                                0 -> false
                                                1 -> true
                                                2 -> true
                                                3 -> true
                                                4 -> true
                                                5 -> true
                                                6 -> false
                                                7 -> false
                                                8 -> false
                                                9 -> false
                                                10 -> false
                                            */
                                    1 2 3 Previous Next