With regular expressions how can I get just the numbers inside brackets?

Alexander Vargas

As an example let's imagine I'm searching for a specific group of number inside a name

[3363]Burn the Witch Year 1958

In this case there are 2 groups of number of the same length, and I need to get the one inside the brackets. How can search for a group of number of a specific length, that are inside a brackets without getting the brackets in the answer?

regexChecker("\\[\\d{3,4}\\]",longString);

The output of the example code is [3347].

The code im using as practice is:

public class PracticaPatrones {
    public static void main(String[] args) {
        String longString = "";

        String[] Names = {"[3358]Maoujou de Oyasumi 1", "[3347]King's Raid; Ishi wo Tsugumono-tachi 2", "[3363]Burn the Witch 3", "[3283]Hachi-nan tte, Sore wa Nai deshou! 1", "[391]Denpa Onna to Seishun Otoko 1", "[0587] Onii-chan Dakedo Ai Sae Areba Kankeinai yo ne 11", "[870] Onii-chan Dakedo Ai Sae Areba Kankeinai yo ne 11"};

        System.out.println(Names.length);
        System.out.println("Bandera");

        for (int i = 0; i < Names.length; i++) {
            longString = Names[i];

            regexChecker("\\[\\d{3,4}\\]", longString);
        }
    }

    public static void regexChecker(String theRegex, String str2Check) {
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher(str2Check);

        while (regexMatcher.find()) {
            if (regexMatcher.group().length() != 0) {
                System.out.println(regexMatcher.group().trim());
            }

            System.out.println("start index : " + regexMatcher.start());
            System.out.println("end index : " + regexMatcher.end());
        }
    }
}
Live and Let Live

You can use the regex, (?<=\[)\d{3,4}(?=\]) which can be explained as follows:

  1. (?<=\[) looks behind for [
  2. \d{3,4} looks for 3 to 4 digits (it's already there in your question)
  3. (?=\]) looks ahead for ]

Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("(?<=\\[)\\d{3,4}(?=\\])");
        Matcher matcher = pattern.matcher("[3358]Maoujou de Oyasumi 1");
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

Output:

3358

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I match URLs with regular expressions?

분류에서Dev

How can I Strip all regular html tags except <a></a>, <img>(attributes inside) and <br> with javascript?

분류에서Dev

How can I embrace the content of all cells in a column with brackets?

분류에서Dev

How can I check for an @ in a regular expression?

분류에서Dev

How can I address the path/file I just specified again?

분류에서Dev

how can i get the prime numbers (Goldbach's Conjecture in c# windows form application)

분류에서Dev

How can I use vector for entering numbers?

분류에서Dev

How can I get and change a value of a line inside of a file if the value of such line is unknown by me?

분류에서Dev

How can I join these 25 INSERT queries into just one?

분류에서Dev

Regular Expressions for phone numbers and mobiles doesn'nt work correctly

분류에서Dev

How can I get a RejectedExecutionException

분류에서Dev

How can I get a solution for this?

분류에서Dev

How can I remove regular months' obervations in an unstructured series in R?

분류에서Dev

How could I get the last occurrence of [0] using regular expression?

분류에서Dev

How can I get $_GET values to a variable

분류에서Dev

My CenturyLink fiber modem is bridged - can I just get rid of it and use my Cisco ASA 5505?

분류에서Dev

I just moved the close/minimize/maxmize buttons to the right, how can I undo the action?

분류에서Dev

I just moved the close/minimize/maxmize buttons to the right, how can I undo the action?

분류에서Dev

How can I remove the decimal parts from numbers in Kotlin?

분류에서Dev

How can I encode and decode an Array of Numbers in Objective-C?

분류에서Dev

How can I zero-pad numbers in a Batch FOR loop?

분류에서Dev

How can I hide the line numbers in Notepad++?

분류에서Dev

How can i store 2 numbers in a 1 byte char?

분류에서Dev

How do i get the page numbers from the example (with PHP)

분류에서Dev

Can these two regular expressions (match and replace) be combined into a single operation?

분류에서Dev

How can I format this div without the text inside breaking a line?

분류에서Dev

How can I sort this object by 'sortby' key which inside an array?

분류에서Dev

How can I inspect if an element inside a cell is checked?

분류에서Dev

How can i check every alphabet inside one string variable?

Related 관련 기사

  1. 1

    How do I match URLs with regular expressions?

  2. 2

    How can I Strip all regular html tags except <a></a>, <img>(attributes inside) and <br> with javascript?

  3. 3

    How can I embrace the content of all cells in a column with brackets?

  4. 4

    How can I check for an @ in a regular expression?

  5. 5

    How can I address the path/file I just specified again?

  6. 6

    how can i get the prime numbers (Goldbach's Conjecture in c# windows form application)

  7. 7

    How can I use vector for entering numbers?

  8. 8

    How can I get and change a value of a line inside of a file if the value of such line is unknown by me?

  9. 9

    How can I join these 25 INSERT queries into just one?

  10. 10

    Regular Expressions for phone numbers and mobiles doesn'nt work correctly

  11. 11

    How can I get a RejectedExecutionException

  12. 12

    How can I get a solution for this?

  13. 13

    How can I remove regular months' obervations in an unstructured series in R?

  14. 14

    How could I get the last occurrence of [0] using regular expression?

  15. 15

    How can I get $_GET values to a variable

  16. 16

    My CenturyLink fiber modem is bridged - can I just get rid of it and use my Cisco ASA 5505?

  17. 17

    I just moved the close/minimize/maxmize buttons to the right, how can I undo the action?

  18. 18

    I just moved the close/minimize/maxmize buttons to the right, how can I undo the action?

  19. 19

    How can I remove the decimal parts from numbers in Kotlin?

  20. 20

    How can I encode and decode an Array of Numbers in Objective-C?

  21. 21

    How can I zero-pad numbers in a Batch FOR loop?

  22. 22

    How can I hide the line numbers in Notepad++?

  23. 23

    How can i store 2 numbers in a 1 byte char?

  24. 24

    How do i get the page numbers from the example (with PHP)

  25. 25

    Can these two regular expressions (match and replace) be combined into a single operation?

  26. 26

    How can I format this div without the text inside breaking a line?

  27. 27

    How can I sort this object by 'sortby' key which inside an array?

  28. 28

    How can I inspect if an element inside a cell is checked?

  29. 29

    How can i check every alphabet inside one string variable?

뜨겁다태그

보관