# Matching Simple Patterns

## Match a single digit character using [0-9] or \d (Java)

`[0-9]`

and `\d`

are equivalent patterns (unless your Regex engine is unicode-aware and `\d`

also matches things like ②). They will both match a single digit character so you can use whichever notation you find more readable.

Create a string of the pattern you wish to match. If using the \d notation, you will need to add a second backslash to escape the first backslash.

Create a Pattern object. Pass the pattern string into the compile() method.

Create a Matcher object. Pass the string you are looking to find the pattern in to the matcher() method. Check to see if the pattern is found.

## Match any float

This will match any signed float, if you don't want signs or are parsing an equation remove `[\+\-]?`

so you have `\d+(\.\d+)?`

Explanation:

`\d+`

matches any integer`()?`

means the contents of the parentheses are optional but always have to appear together- '\.' matches '.', we have to escape this since '.' normally matches any character

So this expression will match

## Matching leading/trailing whitespace

# Trailing spaces

`\s*$`

: This will match any (`*`

) whitespace (`\s`

) at the end (`$`

) of the text

# Leading spaces

`^\s*`

: This will match any (`*`

) whitespace (`\s`

) at the beginning (`^`

) of the text

## Remarks

`\s`

is a common metacharacter for several RegExp engines, and is meant to capture whitespace characters (spaces, newlines and tabs for example). **Note**: it probably *won't* capture all the unicode space characters. Check your engines documentation to be sure about this.

## Matching various numbers

`[a-b]`

where a and b are digits in the range `0`

to `9`

Matching multiple digits

The `\d`

in the above examples can be replaced with a number range:

You can also select specific digits:

Matching numbers in ranges that contain more than one digit:

Matching numbers that divide by other numbers:

matching numbers that divide by 4 - any number that is 0, 4 or 8 or ends in 00, 04, 08, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92 or 96

This can be shortened. For example, instead of using `20|24|28`

we can use `2[048]`

. Also, as the 40s, 60s and 80s have the same pattern we can include them: `[02468][048]`

and the others have a pattern too `[13579][26]`

. So the whole sequence can be reduce to:

Matching numbers that don't have a pattern like those divisible by 2,4,5,10 etc can't always be done succinctly and you usually have to resort to a range of numbers. For example matching all numbers that divide by 7 within the range of 1 to 50 can be done simple by listing all those numbers:

## Selecting a certain line from a list based on a word in certain location

I have the following list:

I want to select the first name of the guys with the Yaron surname.

Since I don't care about what number it is I'll just put it as whatever digit it is and a matching dot and space after it from the beginning of the line, like this: `^[\d]+\.\s`

.

Now we'll have to match the space and the first name, since we can't tell whether it's capital or small letters we'll just match both: `[a-zA-Z]+\s`

or `[a-Z]+\s`

and can also be `[\w]+\s`

.

Now we'll specify the required surname to get only the lines containing Yaron as a surname (at the end of the line): `\sYaron$`

.

Putting this all together `^[\d]+\.\s[\w]+\sYaron$`

.

Live example: https://regex101.com/r/nW4fH8/1