Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression of a Specific Word

Tags:

java

regex

I want to create a regular expression in java using standard libraries that will accommodate the following sentence:

12 of 128

Obviously the numbers can be anything though... From 1 digit to many

Also, I'm not sure how to accommodate the word "of" but I thought maybe something along the lines of:

[\d\sof\s\d]
like image 892
ThreaT Avatar asked Oct 05 '12 12:10

ThreaT


2 Answers

You want this:

\d+\sof\s\d+

The relevant change from what you already had is the addition of the two plus signs. That means, that it should match multiple digits but at least one.

Sample: http://regexr.com?32cao

like image 24
Daniel Hilgarth Avatar answered Sep 28 '22 09:09

Daniel Hilgarth


This should work for you:

(\d+\s+of\s+\d+)

This will assume that you want to capture the full block of text as "one group", and there can be one-or-more whitespace characters in between each (if only one space, you can change \s+ to just \s).

If you want to capture the numbers separately, you can try:

(\d+)\s+of\s+(\d+)
like image 191
newfurniturey Avatar answered Sep 28 '22 09:09

newfurniturey