Published on

# Regex for Height in cm

Height is a measure of how tall an object or person is. It is typically measured in units of length, such as inches or centimeters. In this article let's understand how we can create a regex for matching height in cms from a string and how regex can be matched for height in cms.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

# Structure of Height in centimeter (cm)

• It can be followed by a .
• It can optionally have a decimal digits after the .
• It should be accompanied by a unit of measurement cm

# Regex for matching Height in centimeter (cm) from a string

Regular Expression-

/\d+\.{0,1}\d{1,3}cm$/gm  Test string examples for the above regex- Input StringMatch Output 1233does not match You are 170cm tallmatches randomdoes not match 22.31cmmatches Here is a detailed explanation of the above regex- /\d+\.{0,1}\d{1,3}cm$/gm

\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
{0,1} matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
cm matches the characters cm literally (case insensitive)
$asserts position at the end of a line Global pattern flags i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z]) g modifier: global. All matches (don't return after first match) m modifier: multi line. Causes ^ and$ to match the begin/end of each line (not only begin/end of string)


Hope this article was useful to match height in centimeter(cm) regex pattern.