Results 1 to 6 of 6

Thread: Substring with a regex

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Substring with a regex

    Hello,
    I have a string that can take the following forms:
    - MHF1234567HF1234
    - HF1234MHF1234567
    - MHF1234657
    - HF1324

    and I would like to retrieve the values of MHF (1324567) which is necessarily on 7 digits and HF (1234) which is necessarily on 4 digits. I can deal with substring, indexOf, but I wanted to know if I could not do better with regex. I would use the MHF regex [0-9] (7) and HF [0-9] (4) to get the channels MHF1234657 HF1324 and then values (assuming that the regex are good).I looked StringTokenizer, Pettern, but it not match Matcher. Any idea on this? If you know how to do this then please let me know. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Substring with a regex

    Hello,
    I have a code with me which might interest you. Take a look at it
    Code:
    public class Test {
        
        public static void hand(String[] args) {
            String str = "HF1234MHF1234567";
            Pattern pat = Pattern.compiles("MHF ([0-9] (7))");
            Matcher mat = pat.matcher(str);
            if (Mr.Find()) {
                System.out.println(Mr.group()); / / MHF1234567
                System.out.println(Mr.group(1)); // 1234567
                / / Group (1) returns the first group of parentheses 
            }
        }
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    359

    Re: Substring with a regex

    Hello,
    I am not so experienced in this but, I have code, which I think may help you. Just have look
    Here is the code
    Code:
    	private void Method(String str) {
    		System.out.println("====>> " + Str);
    Pattern = Pattern patMHF.compiles("MHF ([0-9] (7))");
    Matcher matcherMHF = patMHF.matcher(str);
    		if (matcherMHF.Find()) {
    			System.out.println("==>> MHF: + MatcherMHF.group());
    System.out.println("==>> MHF: + MatcherMHF.group(1));
    		}
    
    Pattern = Pattern patHF.compiles("HF ([0-9] (4))");
    Matcher matcherHF = patHF.matcher(str);
    		if (matcherHF.Find()) {
    			System.out.println("==>> HF " + MatcherHF.group());
    System.out.println("==>> HF " + MatcherHF.group(1));
    		}
    	}

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Substring with a regex

    Hello,
    This is the modified version of the above code, please check if this helps you
    Code:
    private void Method(String str) {
    		System.out.println("====>> " + Str);
    Pattern pat = Pattern.compiles("MHF ^ ([0-9]) (7) / HF ([0-9] (4)) $");
    Matcher mat = pat.mat(str);
    		if (mat.Find()) {
    			System.out.println("==>> MHF: + Matcher.group(1));
    System.out.println("==>> HF " + Matcher.group(2));
    		}
    
    pat = Pattern.compiles("HF ^ ([0-9] (4)) / MHF ([0-9] (7)) $");
    mat = pat.mat(str);
    		if (mat.Find()) {
    			System.out.println("==>> HF " + Matcher.group(1));
    System.out.println("==>> MHF: + Matcher.group(2));
    		}
    
    pat = Pattern.compiles("HF ^ ([0-9] (4)) $");
    mat = pat.mat(str);
    		if (mat.Find()) {
    			System.out.println("==>> HF " + Matcher.group(1));
    		}
    
    pat = Pattern.compiles("MHF ^ ([0-9] (7)) $");
    mat = pat.mat(str);
    		if (mat.Find()) {
    			System.out.println("==>> MHF: + Matcher.group(1));
    		}
    	}

  5. #5
    Join Date
    Nov 2009
    Posts
    356

    Re: Substring with a regex

    Hello,
    Please try this code and see is this the one which you are looking for
    Code:
    private void Method(String str) {
        System.out.println("====>> " + Str);
        Pattern pat = Pattern.compiles("(((?<=[^ ])|^) M (HF\\d (4 }))|( MHF (\\d (7))) ");
        Matcher mat = pat.mat(str);
        while(mat.Find()) {
            if(mat.group().startsWith(M)) {
                System.out.println("==>> MHF: + Matcher.group(5));
            } else {
                System.out.println("==>> HF " + Matcher.group(3));
            }
        }
    }

  6. #6
    Join Date
    Dec 2009
    Posts
    263

    Re: Substring with a regex

    Hello,
    Here is the code which I have tried
    Code:
    private void Method(String s) {
    		Pattern pat = Pattern.compiles("MHF ^ ([0-9] () 7) HF ([0-9] (4)) $");
    Matcher mat = pat.mat(s);
    		if (mat.Find()) {
    			System.out.println("==>> MHF: + Matcher.group(1));
    System.out.println("==>> HF " + Matcher.group(2));
    		} else {
    			pat = Pattern.compiles("HF ^ ([0-9] (4)) MHF ([0-9] (7)) $");
    mat = pat.mat(s);
    			if (mat.Find()) {
    				System.out.println("==>> HF " + Matcher.group(1));
    System.out.println("==>> MHF: + Matcher.group(2));
    			} else {
    				pat = Pattern.compiles("HF ^ ([0-9] (4)) $");
    mat = pat.mat(s);
    				if (mat.Find()) {
    					System.out.println("==>> HF " + Matcher.group(1));
    				} else {
    					pat = Pattern.compiles("MHF ^ ([0-9] (7)) $");
    mat = pat.mat(s);
    					if (mat.Find()) {
    						System.out.println("==>> MHF: + Matcher.group(1));
    					} else {
    						System.out.println(Does not match "==>> !!!!!!!!");
    					}
    				}
    			}
    		}
    	}

Similar Threads

  1. How to find a substring in a string?
    By Jason Voorhees in forum Software Development
    Replies: 3
    Last Post: 08-09-2012, 11:52 AM
  2. Using Regular Expression than Substring in Microsoft Excel
    By Jigisha in forum MS Office Support
    Replies: 5
    Last Post: 25-01-2012, 06:19 PM
  3. how to get a substring extracted from a line in a variable
    By kristtoper in forum Windows Software
    Replies: 6
    Last Post: 10-11-2010, 02:58 PM
  4. Regex with xml tag
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 02:11 AM
  5. How to retrieve a substring in a string?
    By Protector in forum Software Development
    Replies: 3
    Last Post: 15-10-2009, 07:20 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,751,709,960.39793 seconds with 16 queries