c# - Regular expression to match a string that contains only numbers, not letters


My code is currently using the following Regex expression which matches on numbers:

Regex numberExpression = new Regex(@"(?<Number>\d+)");

This current works fine for input strings like "1", "100", "1a", "a1", etc....

But I want to change it so it does NOT match when the input string contains a letter, so "1", "100" would match, but "1a", "a1", would not.

Can anyone help, I know this is a simple regular expression question but I can't get my head around the forward and backward looking. I have tried:

Regex numberExpression = new Regex(@"(?<Number>^![a-zA-Z]\d+![a-zA-Z])");

but that didn't work, and fails to match any of the above input.

2 Answers: 

You are trying to do it the hard way, by looking for a numeric substring of the input, and then looking to see that there isn't anything before or after that substring.

The easy way to do it is to force the regular expression to either match the entire input string or nothing:

Regex numberExpression = new Regex(@"^\d+$");

where "^" means "beginning of line" and "$" means "end of line".


Regex is overkill. Try this:


More Articles

functional programming - Mix pure function with side effect in scala

I have a simple api method:def addWishToBoard(wish: Wish, boardId: BoardId, userId: UserId): Option[Board]It adds wish to board, if boardId and userId matches.Boards are stored simply in a list:private var boards = List.empty[Board]I use var here in order to simulate side effect.The implementation c

php - get_footer not showing for Wordpress site

For some reason my footer disappeared for my site http://juliannaspizza.com. I checked the template file for the home page and get_footer is being called as shown below. Any idea why my footer is still not showing up? <?php /** * Template Name: Full Width Page * * @package Fortune */ get_he

django - R_user not defined , rpy2

I want to use R from django app .Now I am in a huge mess .--> I have installed rpy2 for that .---> I am able to run everything from Python IDE eg . import rpy2.rinterface as rinterfacerinterface.initr()or import rpy2.robjects as somethingAs I open Python from cmd or in django file .I am getting err

php - Is it possible to tell the names of the $_GET variables the page is waiting for?

Hello I have a question about PHP $_POST and $_GET.Let's say I have a web page that is expecting $_GET variables.Are the variable names anonymous?Let's say index.php has the script ofif( isset( $_GET['somevariale'] ) ){ rest of the code}Is it possible for anyone to find out the name of the $_GET va

android - the app falls when i select a user from a SQLITE list and pass the ID to another activity

i have a minor problem here, the app i created makes the user adds contacts details of his own and can call them again by pressing on them from the listview and thier data should appear on the new activity to edit or delete themthe problem is that the app crashes when i select a contact....where did

cookies - PHP Unique Computer ID

Is it possible for php(or javascript in the worst case) to create a unique id for a user that is not cookie or ip dependant. I have seen on myminicity.com that on each city the count only goes up once a day(it has a unique id for everyone i think) and even if I delete my cookies and refresh ip it st

javascript - jQuery .load: any way to load the same page when page is refreshed

So I have a website that loads pages to a container div:function goto(addr) { $("#content").load(addr);}and a link that executes it<a href="#" id="aboutus" onclick="goto('page/aboutus.php');">About us</a>My problem is that whenever the page is refreshed, the loaded content resets to t

specifications - Essential techniques for pinpointing missing requirements?

An initial draft of requirements specification has been completed and now it is time to take stock of requirements, review the specification. Part of this process is to make sure that there are no sizeable gaps in the specification. Needless to say that the gaps lead to highly inaccurate estimates,

reactjs - What is the benefit of @emotion/core over emotion for a React project?

If I install emotion then the API is nice and clear: package.json:"dependencies": { "emotion": "^10.0.9",React component: import React from "react";import { css } from "emotion";const someStyle = css` display: none;`function MyComponent() { return ( <div className={someStyle} /> );}Ho

php - how to set a page active automatically when it is loaded

i want to change the active class when each page loaded. my jquery code is$(document).ready(function () {$('.nav li a').click(function(e) { $('.nav li a.active').removeClass('active'); $(this).addClass('active'); e.preventDefault();});});and my html code is<ul class="nav nav-stacked bg-n