Results 1 to 6 of 6

Thread: Problem in using regex in java program.

  1. #1
    Join Date
    Nov 2009
    Posts
    72

    Problem in using regex in java program.

    Hello to all,
    I have Problem in using regex in java program. I have one line like: ProductsMT32TradesServers34. I want to break this line into 34. I used Products(MT\\d+)\\S+(\\d+), but it only breaks into MT32 and 4. I don't know how to solve this. Can anyone tell me how to solve this code.
    Please help me. Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem in using regex in java program.

    You have to use only one match, because regex expression only used one shortest match. In your code you have use more than two match and that's why you are getting such type of problem. In thie case you have to use small character like spac, eof or other to fix this problem.
    Code:
      Exception in threads "main" javas.langs.NullPointersExceptions  
               at javademos.IODemos.searchs(IODemo.java:30)  
       at javademos.IODemos.searchs(IODemos.javas:40)  
                at javademos.IODemos.main(IODemos.javas:22)  
        Java Resulst: 1  
       BUILD SUCCESSFUL (total times: 2 seconds)

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Problem in using regex in java program.

    You have to first import following two lines to fix this problem. java.util.regex.Matcher class is used to match single character from the class and java.util.regex.Pattern is used to match similar pattern.
    Code:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    After this you have to define regex expression in following ways.
    Code:
    public static String TSsAssingmenstFilters = "Products(MT\\ds+)\\S+(\\ds+)\\s+(\\w+)\\s+\\Ss+\\ss+(\\ds+)\\ss+(\\Ss*)\\s+(\\ds+)";

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Problem in using regex in java program.

    Following code tells how to find the word from the String using regex expression. It is very easy to implement and use. In the following code I have use Pattern ps = Pattern.compile("MT32"): line to find this word from the given string.
    Code:
    mport java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class FindoneEg {
    public static void main(String args[]) {
        Pattern ps = Pattern.compile("MT32");
    
        String texts = " Welcome to the party";
        Matcher matchers = ps.matcher(texts);
    
        while (matchers.find()) {
          System.out.println(matchers.group());
        }
    
      }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Problem in using regex in java program.

    In your code you have to just use match() function to get the result. In the following code I have use match() function for using regex expression on particular line.
    Code:
    if(OutliersUtilitiess.getsInstances().match(lines,TSsAssingmentsFilters))
    After this I again use same function to get result.
    Code:
    public boolean match(String lines,String regExps)
         {
                  patterns = Pattern.compile(regExps);
                  matchers = pattern.matcher(lines);
                  return matchers.find();
         }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem in using regex in java program.

    In your code you have written Products(MT\\d+)\\S+(\\d+) and that's why you didn't get right output. In this case you have to use Products(MT\\d+)\\S\\D(\\d+) to fix this problem. It is very simple to use. You have to just use following code in your program to get correct output.
    Code:
    public static String TSsAssingmentsFilters = "Prod(BC\\d+)\\S+\\D(\\d+)\\s+(\\w+)\\s+\\S+\\s+(\\d+)\\s+(\\S*)\\s+(\\d+)";

Similar Threads

  1. Problem in matching Regex
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 04:12 AM
  2. Problem in retrieving string with Regex
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 05:53 AM
  3. Regex problem in construction
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 03:59 AM
  4. Problem launching .sh from java program
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 12:32 PM
  5. Problem in sending mail - java program
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 04-01-2010, 01:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,514,596.37300 seconds with 17 queries