Java pattern for numbers

Extract Numbers From String Using Java Regular Expressions

Last updated: 11 February 2020 The following are examples which show how to extract numbers from a string using regular expressions in Java. Being able to parse strings and extract information from it is a key skill that every tester should have. This is particularly useful when testing APIs and you need to parse a JSON or XML response. The following Java Regular Expression examples focus on extracting numbers or digits from a String.

Extract All Numbers from a String

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples < public static void main(String[]args) < Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher("string1234more567string890"); while(m.find()) < System.out.println(m.group()); >> > 

Extract nth Digit from a String

If you want to extract only certain numbers from a string you can provide an index to the group() function.

For example, if we wanted to only extract the second set of digits from the string string1234more567string890 , i.e. 567 then we can use:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples < private static final Pattern p = Pattern.compile("[^\\d]*[\\d]+[^\\d]+([\\d]+)"); public static void main(String[] args) < // create matcher for pattern p and given string Matcher m = p.matcher("string1234more567string890"); // if an occurrence if a pattern was found in a given string. if (m.find()) < System.out.println(m.group(1)); // second matched digits >> > 

Explanation of the Pattern [^\d]*[\d]+[^\d]+([\d]+)

  • ignore any non-digit
  • ignore any digit (first number)
  • again ignore any non-digit
  • capture the second number
Читайте также:  Возможности питон мой код

Extract Number from a Tag Attribute

When dealing with XML or HTML tags, sometimes there is a need to extract a value from an attribute. For example, consider the following tag

To extract number 9999 we can use the following code:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples < public static void main(String[]args) < Pattern pattern = Pattern.compile("numFound=\"(8+)\""); Matcher matcher = pattern.matcher(""); if (matcher.find()) < System.out.println(matcher.group(1)); >> > 

Extract a String Containing digits and Characters

You can use Java regular expressions to extract a part of a String which contains digits and characters. Suppose we have this string Sample_data = YOUR SET ADDRESS IS 6B1BC0 TEXT and we want to extract 6B1BC0 which is 6 characters long, we can use:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples < public static void main (String[] args) < Pattern p = Pattern.compile("YOUR SET ADDRESS IS\\s+([A-Z0-9])"); Matcher n = p.matcher("YOUR SET ADDRESS IS 6B1BC0 TEXT"); if (n.find()) < System.out.println(n.group(1)); // Prints 123456 >> > 

Extract Key-Value Pairs With Regular Expressions

Let’s suppose we have a string of this format bookname=testing&bookid=123456&bookprice=123.45 and we want to extract the key-value pair bookid=123456 we would use:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples < public static void main(String[] args) < String s = "bookname=cooking&bookid=123456&bookprice=123.45"; Pattern p = Pattern.compile("(?<=bookid=)\\d+"); Matcher m = p.matcher(s); if (m.find()) < System.out.println(m.group()); >> > 

Источник

Numbers only regex (digits only) Java

Numbers only (or digits only) regular expressions can be used to validate if a string contains only numbers.

Basic numbers only regex

Below is a simple regular expression that allows validating if a given string contains only numbers:

Enter a text in the input above to see the result

Real number regex

Real number regex can be used to validate or exact real numbers from a string.

Enter a text in the input above to see the result

 > 

Enter a text in the input above to see the result

Notes on number only regex validation

In Java you can also validate number by trying to parse it:

 catch (NumberFormatException nfe)

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Источник

Number Patterns in Java

Number Patterns in Java

Number Patterns are quite a trend for freshers to get as part of interview questions as it provides a good brainstorming session to analyze a person’s creativity and innovation. Solving more of patterns show logical and mathematical capability. It is indeed a good way to form different patterns using all these conditional loops and syntaxes in java. It helps in improvising the skills of optimization and helps develop logical and analytical aptitude. It can be substituted with any character or symbol. If you learn number pattern, you can frame any of the patterns based on java.

Top Examples of Number Patterns

Here we will learn how to face some of the good Number Patterns. Let’s have a peek at some of the good Number Patterns in java with examples and code implementation, which are explained below in detail:

Example #1

import java.util.Scanner; public class Pattern1 < public static void main (String [] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows "); int rows = scanner.nextInt(); System.out.println("Printing Pattern"); for (int i = 1; i System.out.println(); > > >

Number Patterns in Java eg1

Example #2

import java.util.Scanner; public class Pattern2 < public static void main (String [] args) < < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); System.out.println("Printing Pattern"); for (int i = 1; i System.out.println(); > > > >

Number Patterns in Java eg2

Example #3

import java.util.Scanner; public class Pattern_3 < public static void main (String [] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); System.out.println("Printing Pattern"); for (int i = 1; i System.out.println(); > for (int i = rows; i >= 1; i--) < for (int j = 1; j < i; j++) < System.out.print(j + " "); >System.out.println(); > > >

Number Patterns in Java eg3

Example #4

import java.util.Scanner; public class Pattern4 < public static void main(String[] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); System.out.println("Printing Pattern"); for (int i = rows; i >= 1; i--) < for (int j = 1; j System.out.println(); > for (int i = 1; i <= rows; i++) < for (int j = 1; j System.out.println(); > > >

Number Patterns in Java eg4

Example #5

import java.util.Scanner; public class Pattern5 < public static void main(String [] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); System.out.println("Printing Pattern"); for (int i = rows; i >= 1; i--) < for (int j = i; j >= 1; j--) < System.out.print(j + " "); >System.out.println(); > for (int i = 1; i = 1; j--) < System.out.print(j + " "); >System.out.println(); > > >

Number Patterns in Java eg5

Example #6

import java.util.Scanner; public class Pattern6 < public static void main(String[] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows "); int rows = scanner. nextInt(); System.out.println("Printing Pattern"); for (int i = 1; i i; j--) < System.out.print(" "); >for (int k = 1; k System.out.println(); > > >

Number Patterns in Java eg6

Example #7

import java.util.Scanner; public class Pattern7 < public static void main (String [] args) < Scanner scanner = new Scanner (System.in); System.out.println("Enter the number of rows "); int rows = scanner.nextInt(); System.out.println("Printing Pattern"); for (int i = 1; i = i; j--) < System.out.print(j + " "); >System.out.println(); > > >

eg7

Example #8

import java.util.Scanner; public class Pattern8 < public static void main (String[] args) < Scanner scanner = new Scanner (System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); System.out.println("Printing Pattern"); for (int i = rows; i >= 1; i--) < for (int j = rows; j >= i; j--) < System.out.print(j + " "); >System.out.println(); > > >

eg8

Example #9

import java.util.Scanner; public class Pattern9 < public static void main(String[] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); System.out.println("Printing Pattern "); for (int i = rows; i >= 1; i--) < for (int j = 1; j System.out.println(); > > >

eg9

Example #10

import java.util.Scanner; public class Pattern10 < public static void main(String [] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); int k = 1; System.out.println("Printing Pattern"); for (int i = 1; i System.out.println(); > > >

eg10

Example #11

import java.util.Scanner; public class Pattern11 < public static void main(String[] args) < Scanner scanner = new Scanner (System.in); System.out.println("Enter number of rows"); int rows = scanner.nextInt(); System.out.println("Printing Pattern"); for (int i = 1; i = 1; j--) < System.out.print(j + " "); >System.out.println(); > > >

Number Patterns in Java eg11

Example #12

import java.util.Scanner; public class Pattern12 < public static void main(String[] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); System.out.println("Pattern Printing"); for (int i = 1; i = 1; j--) < System.out.print(temp + " "); temp = temp + rows; >System.out.println(); > > >

Number Patterns in Java eg12

Example #13

import java.util.Scanner; public class Pattern13 < public static void main(String[] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = scanner.nextInt(); System.out.println("Pattern Printing"); for (int i = 1; i i; j--) < System.out.print(" "); >int temp= 1; for (int k = 1; k System.out.println(); > > >

Number Patterns in Java eg13

Example #14

import java.util.Scanner; public class Pattern14 < public static void main(String[] args) < Scanner scanner = new Scanner(System.in); System.out.println("Enter number of rows"); int rows = scanner.nextInt(); System.out.println("Pattern Printing"); for (int i = 1; i for (int k = i - 1; k >= 1; k--) < System.out.print(k + " "); >System.out.println(); > > >

Number Patterns in Java eg14

Conclusion

Solving number Patterns or any design pattern improves a person’s analytical and logical building aptitude. In bigger domains, it provides an overview of how to create and fulfil requirements given for a project and how effectively it can be tackled with confidence.

This is a guide to Number Patterns in Java. Here we discuss the introduction and top 14 examples of number patterns in java along with its code implementation. You may also look at the following articles to learn more-

89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8

Источник

Оцените статью