Andra
Andra

Reputation: 1382

Regular expression to count <>

So I've a case like this

<> = 1
<><> = 2
<<>> = 2
<<test<> = 1

How do I find all of the "<>" it's inside the "<>" as well using regular expression?

Here's code that I've tried.

import java.io.IOException;
import java.util.regex.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);
        String input = s.nextLine();

        Pattern p = Pattern.compile("<(.*?)>");
        Matcher m = p.matcher(input);

        int count = 0;
        while(m.find()){
            count++;
        }

        System.out.println(count);
    }
}

Upvotes: 0

Views: 590

Answers (3)

The Scientific Method
The Scientific Method

Reputation: 2436

start with count variable and empty stack . You should solve this using stack, iterate through each character, when you find < push it into the stack, the time you find > , pop from the stack while the stack is not empty and increase your count .

Edit: using stack

import java.util.*;
public class Test {
      public static void main(String[] args) { 
      Stack<String> myStack = new Stack<String>();
      String str = "<<test<> = 1 <><>";
      int count=0;
      char[] chs = str.toCharArray();    
          for(char ch: chs){
            if(ch == '<'){
                myStack.push(String.valueOf(ch));               
            }
            if( !myStack.isEmpty() & (ch == '>')){
                myStack.pop();
                count++;
            }
          }
      System.out.println("count = "+count); 

      }
    }

output

count = 3

Upvotes: 1

lopoto
lopoto

Reputation: 276

You can't do that easily with a regular expression. A finite automaton is the machine that recognizes a regular expression, his memory is finite and thus cannot deal with unknowns levels of nesting.

You have to make a regex that matches a fixed depth.

Or alternatively you can make your own algorithm like so, this is very basic:

import java.lang.Math;
import java.util.*;


public class HelloWorld {
    public static void main(String[] args) {
        String s = "<<test<>";
        List <Character> l = new ArrayList <Character>();
        int count = 0;

        for (char e: s.toCharArray()) {
            if (e == '<') {
                l.add(e);
            } else if (e == '>') {
                if (l.size() > 0) {
                    l.remove(l.size() - 1);
                    count++;
                }
            }
        }

        System.out.println(count);
    }
}

Upvotes: 1

Amadan
Amadan

Reputation: 198324

You can't do that with Java's regular expressions, without also employing recursion. However, a simple counting scheme works: start with level = 0, count = 0, then iterate through characters. For each <, increase level. For each >, decrease level and increment count. If level is ever negative, abort (possibly with an error), or ignore that character (depending on how you want to handle cases like <>><<>).

Upvotes: 1

Related Questions