Reputation: 627
I have a description text file with content constructed in such manner:
Book title - (number)
Currently user needs to find book title, then reads the book number and looks for the appropriate file (number.txt is a file name of book).
I want to use Regexp class to extract "(number)" expression. My sample code doesn't work (returns TRUE even if "( )" don't exist:
Regex r = new Regex("([0-9])");
Could you help me to construct correct RegExp?
Upvotes: 0
Views: 130
Reputation: 27506
You should escape the parentheses:
Regex r = new Regex(@"\([0-9]\)");
And if number
contains more that one digit, you should add +
:
Regex r = new Regex(@"\([0-9]+\)");
Or
Regex r = new Regex("\\([0-9]+\\)");
Upvotes: 4
Reputation: 12630
At the moment you are only looking for a single digit, try this:
Regex r = new Regex("\([0-9]+\)");
Which searches for one or more digits in the range 0-9. Escaping the parentheses will also ensure that only numbers between the brackets are found.
Upvotes: 0