Results 1 to 6 of 6

Thread: Regex - use word as a delimiter

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Regex - use word as a delimiter

    Hello,
    It exists in the Java API, for example, a scanner class
    Code:
    String input = "1 fruit 2 fruit red fruit blue fruit";
         Scanner s = new Scanner(input);
         s.findInLine("(\\d +) fruit (\\d +) fruit (\\w +) fruit (\\w +) ");
         MatchResult rs = s.Match();
         for (int i =1; I <= rs.GroupCount(); I + +)
             System.out.System.out.println(rs.group(i);
         s.close();
    I was wondering if it would not be possible in the given example, modify the regular expression by putting "(sth fruit) *" since the word acts a the delimiter. Do you have any idea of how to do it. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Regex - use word as a delimiter

    Hello,
    It depends on what you are looking. In the example the chain has sought to match "<number> <number> fish fisting fish <string> <string>" ... If you pattenr the shortcuts you can not verify the exact scheduling (eg "1 Fish 3 fish red fish blue fish" does not conconde with the pattern of the sample). In this example the method findInLine () returns the entire string (or almost). If you consider that you do not want to differentiate numbers / channels or scheduling, you can do the same thing by calling several times findInLine ():
    Code:
    		String input = "1 fruit 2 fruit red fruit blue fruit";
    Scanner s = new Scanner(input);
    		
    		while ( s.findInLine("(\\w +) fruit ") != null) {
    			MatchResult rs = s.Match();
    System.out.System.out.println(rs.group(1));
    		}

  3. #3
    Join Date
    Dec 2009
    Posts
    202

    Re: Regex - use word as a delimiter

    Hello,
    Even i was thinking for using the regular expression for the same purpose.
    "(\ \ d +) number, (\ \ d +) +, (\ \ d +) number"
    Actually I do not know how many times there are numbers written in the middle, this can vary according to entries.
    Yet in the quantifier they say
    Code:
    X + X, one or more times
    If you have any idea , thank you in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    330

    Re: Regex - use word as a delimiter

    Hello,
    This "(\ \ d +) + 'trek is not correct: this means a decimal value 1 or several times, and this 1 or more times. But without separator can no sense. Should you give some examples of channels that you can have input to make it clearer and the results you want. I hope you are understanding what I am trying to explain you here.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Regex - use word as a delimiter

    Hello,
    Do you know StringTokenizer? It allows you to split a string with a delimiter and you program in pieces in an array. I too understand the data that you can be manipulated, but that the tokenizer can help. If you know about the StringTokenizer's then you should use them, if you do not know them then I recommend you to do some study of the StringTokenizer's and then use them in your code, and see after that your program will be more effective.

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Regex - use word as a delimiter

    Hello,
    Have a look at the following code, this may help you
    Code:
    Public class Hand {
     
        Public static void hand(String[] args) throws Exception {
        	
    		String str = "beginning 1, 2, 3, 47, 589; 9; end";
    String pt = "start (\\d +;) + end ";
    		
    		if (str.games(pt)) {
    			NumMatcher = Pattern Matcher.compiles("\\d + ").matcher(str);
    			while (numMatcher.find()) {
    				System.out.System.out.println("No." + NumMatcher.group());
    			}
    		}
        }
    }

Similar Threads

  1. Excel CSV Export use semi-colon as delimiter not comma
    By Celestial in forum MS Office Support
    Replies: 2
    Last Post: 17-01-2012, 01:22 PM
  2. Substring with a regex
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 02-04-2010, 11:06 AM
  3. Regex to replace word
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 04:34 AM
  4. Regex with xml tag
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 02:11 AM
  5. Query in php regex
    By Linux-Us in forum Software Development
    Replies: 5
    Last Post: 14-12-2009, 12:54 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,714,021,459.42091 seconds with 16 queries