Java Regular Expressions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Maverick22
    SBR Wise Guy
    • 04-10-10
    • 807

    #1
    Java Regular Expressions
    Can someone provide me the regular expression in java that will find spaces (' ') and newline/return characters ('\n') between two pipe ('|') characters. Such that I find two pipe characters that are essentially right beside each other?

    Eg.

    String test = " | \n\n | ";

    or

    String test = " | |";

    For some reason, I am having a hard time figuring this out.

    When i try (on top of a lot of other attempts):

    Pattern.compile("|\s*|");

    It doesnt seem to work... Thanks in advance guys for the reply/fix!!!
  • ScoreProphet
    SBR Rookie
    • 09-01-10
    • 11

    #2
    Don't forget to escape your pipes "\|"
    Comment
    • roasthawg
      SBR MVP
      • 11-09-07
      • 2990

      #3
      Yeah, just escape the pipes and you'll be fine.
      Comment
      • Maverick22
        SBR Wise Guy
        • 04-10-10
        • 807

        #4
        Ok... I should clarify I guess. I am pretty much tryna parse up some html..

        and trying to find the spaces and new lines in between "<tag>some text with spaces and newlines and &nbsp; and alphanumeric, etc<tag>"

        figured using pipes would help abstract the problem. my bad.
        Comment
        • MonkeyF0cker
          SBR Posting Legend
          • 06-12-07
          • 12144

          #5
          There's a little tool called Regex Buddy that is useful for testing/applying regular expression statements.

          P.S. - I could tell you to do it in VB.NET but I'm not sure it would be the same in Java and I'm not at home so I could check it.
          Last edited by MonkeyF0cker; 09-07-10, 08:45 PM.
          Comment
          • ScoreProphet
            SBR Rookie
            • 09-01-10
            • 11

            #6
            It sounds like you're trying to strip the non-essential whitespace, am I right? Shouldn't one of the existing Java HTML parsing libraries be able to do this? If not, or if you'd just rather write it yourself, check out this online regex tester http://gskinner.com/RegExr/
            Comment
            • ScoreProphet
              SBR Rookie
              • 09-01-10
              • 11

              #7
              I'm very green when it comes to regexp's, but maybe try something like this:

              <.+>.*\r+.*?</.+>

              I think that will match an opening tag with text after it, followed by one or more carriage returns (or maybe it's \n instead of \r), followed by more optional text, and finally a closing tag. Does that look right to everybody?
              Comment
              • LucenaPosition
                SBR Rookie
                • 03-06-10
                • 3

                #8
                You have to escape the backslashes in the Pattern for Java. So code like the following should work:

                Pattern pattern = Pattern.compile("\\|\\s*\\|");
                Matcher matcher = pattern.matcher("Heres some | \n\n | text");
                if (matcher.find()) {
                System.out.println("found it ==>" + matcher.group());
                }

                As someone mentioned above, you really should look around for an HTML parsing library instead of attempting to do this with regular expressions.
                Comment
                SBR Contests
                Collapse
                Top-Rated US Sportsbooks
                Collapse
                Working...