If you are trying to write a validation code that can accept only numbers between 0-9 digits, then you can make use of the below RegEx (Regular Expression)
Expression:^[0-9]+$
Explanation:
- ^ : The caret or circumflex is used to assert the start of the string.
- [0-9] : To matches any digit between 0 and 9.
- + : matches the previous character (i.e. [0-9]) one or more times.
- $ : Dollar sign asserts the end of the string.
Example: Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegEx {
public static void main(String[] args) {
final String regex = "^[0-9]+$";
final String str = "100\n"
+ "abc";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Example: Python
import re
# Define the regular expression
regex = r"^[0-9]+$"
# Test the regular expression on some sample strings
strings = ["123", "abc", "12a", "java", "a456", "12.34"]
for string in strings:
if re.match(regex, string):
print(f"{string} matches the regex")
else:
print(f"{string} does not match the regex")
Example: GoLang
package main
import (
"fmt"
"regexp"
)
func main() {
regex := regexp.MustCompile("^[0-9]+$")
strings := []string{"123", "anc", "8910", "123a", "a456", "12.34"}
for _, str := range strings {
if regex.MatchString(str) {
fmt.Printf("%s matches the regex\n", str)
} else {
fmt.Printf("%s does not match the regex\n", str)
}
}
}
Example: PHP
<?php
$regex = "/^[0-9]+$/";
$strings = array("123", "abc", "1a", "123a", "a456", "12.34");
foreach ($strings as $string) {
if (preg_match($regex, $string)) {
echo "$string matches the regex\n";
} else {
echo "$string does not match the regex\n";
}
}
?>
Example: JavaScript
let regex = /^[0-9]+$/;
let strings = ["123", "abc", "122x", "123a", "a456", "12.34"];
for (let string of strings) {
if (regex.test(string)) {
console.log(`${string} matches the regex`);
} else {
console.log(`${string} does not match the regex`);
}
}
Have Questions? Post them here!
- Fix NVIDIA GeForce Experience ERROR CODE 0x0003
- How to convert byte array to String [Kotlin]
- Unzip a Zip file from Terminal Command
- How to write hello world different languages syntax
- [Error] zsh: command not found: mvn
- How to know the version of OpenSSL
- [Solution] IDEA IntelliJ System.out.println function shortcut (sysout alternative for eclipse IDE)
- The default username and password for RabbitMQ
- ChatGPT Outage: Hmm...something seems to have gone wrong. Maybe try me again in a little bit.
- How to check the version of NodeJS installed
- Gmail Unable to upload because it is a folder or a package (like an application bundle or RTFD document)
- How to remove password from pdf file
- BSNL Broadband upgrades speed to minimum 2MBps for all users 512Kbps 1Mbps
- MongoDB: Failed to connect to 127.0.0.1:27017 reason: Connection refused
- Turn off Focus Mode on Mac
- Steps to Delete or Deactivate Instagram Account
- [IRCTC] Indian railways official eRail API 1.1 for developers to get train info
- CentOS Cannot find a valid baseurl for repo base7x86_64 yum
- Why I see Download pre-built shared indexes in IntelliJ
- Copy file from a remote server to current local directory system using SCP command
- [Fix] Minecraft Error: A JNI error has occurred, please check your installation and try again
- How to install Zsh shell
- Merge multiple zip files without unzipping (extracting)
- How to find someone on Instagram
- How to get an embed code from Vimeo?
- Add Line Break in Microsoft Excel Cell on Mac (macOS) - MacOS
- [fix] Cannot connect to the Docker daemon at unix:var/run/docker.sock. Is the docker daemon running? - Docker
- Fix: Amazon Linux bash: sudo: command not found - AWS
- JDK Location in Android Studio - Android-Studio
- How to use SCP Command to Copy Directory - Linux
- Eclipse : Workspace was written with an older version of the product and will be updated - Eclipse
- Java JDBC Delete a Record in Database Table using PreparedStatement - Java
- Spotify is down for iOS and Android globally - error no internet connection available, something went wrong - News