Results 1 to 3 of 3
  1. #1
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Question Matching pattern on String - Problems with word length of regex

    The idea:
    I'm trying to match a text that will be spread all over the HTML page with a regular expression. The text follows the following pattern:
    <!-- BLOCK BEGIN: randomWordHere -->
    <!-- BLOCK BEGIN: totallyRandomWordHere -->
    <!-- BLOCK BEGIN: anotherRandomWordHere -->


    The problem:
    I got it almost working with regex, my problem is the word size. I could match a word like "content" through regex, but i just can't make it match "content" and "contents" at the same time due to word length and i just can't seem to find a way to do this. I'm matching character by character on the regex, and doing this way fixes the length of the word... and i don't want to do that.

    The code:
    Java Code:
            Pattern p = Pattern.compile("<!-- BLOCK BEGIN: [^\\s][^\\s][^\\s][^\\s][^\\s][^\\s][^\\s] -->");
            Matcher m = p.matcher(this.tplCode);
            
            boolean matchFound = m.find(); //true
            int start = m.start();
            int end = m.end();
            out.print("|"+this.tplCode.substring(start, end)+"|");
    Any ideas?

    Thanks in advance!
    Last edited by KingdomX; 01-13-2012 at 01:11 PM. Reason: adding smile, -_-

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Matching pattern on String - Problems with word length of regex

    Umm I don't think you're really getting the point of regular expressions. Hint: You can match something like "any number of characters" very easily using regular expressions. Have a look at the Pattern API.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Matching pattern on String - Problems with word length of regex

    Hmmmmm, thanks.

    I got it working with:
    Java Code:
    Pattern p = Pattern.compile("<!-- BLOCK BEGIN: \\b\\w+\\b -->");
    Matcher m = p.matcher(this.tplCode);
     
    boolean matchFound = m.find(); //true
    int start = m.start();
    int end = m.end();
    out.print("|"+this.tplCode.substring(start, end)+"|");
    Solved!

Similar Threads

  1. Please Help me regarding String pattern matching
    By theyoung in forum New To Java
    Replies: 11
    Last Post: 11-08-2011, 02:59 AM
  2. Replies: 11
    Last Post: 08-15-2011, 10:12 AM
  3. String matching a pattern
    By vividcooper in forum New To Java
    Replies: 7
    Last Post: 01-07-2010, 10:30 PM
  4. Regex - matching literal characters
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-07-2009, 11:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •