regex to extract number and date from string

Dharmesh Mehta

I'm trying to extract date, percentage or number from string. Strings can be:

  1. the response value 10 (from here I want to extract 10)
  2. the response value 10/12/2014 (from here I want to extract 10/12/2014)
  3. the response value 08/2015 (from here I want to extract 08/2015)

I've written regex as (?:\d{2}\/\d{4}|\d{2}(?:\/\d{2}\/\d{4})?) Regex is satisfying 12/12/2014, 10, 02/2012.

I'm also trying to modifying same regex to get 10, 08/2015 and 10/10/2015 but not getting how to get.

How can this be achieved?

The fourth bird

To match your example data, you could use an alternation matching either 2 digits / 4 digits, or match 2 digits with an optional part that matches 2 digits and 4 digits.

\b(?:\d{2}\/\d{4}|\d{2}(?:\/\d{2}\/\d{4})?)\b

Explanation

  • \b Word boundary, prevent the word char being part of a larger word
  • (?: Non capture group
    • \d{2}\/\d{4} Match 2 digits/4 digits
    • | Or
    • \d{2} Match 2 digits
    • (?:\/\d{2}\/\d{4})? Optionally match /2 digits/4 digits
  • ) Close group
  • \b Word boundary

Regex demo

Note that 2 and 4 digits could also match 99 and 9999. If you want to make your match more specific, this page can be helpful https://www.regular-expressions.info/dates.html

const pattern = /\b(?:\d{2}\/\d{4}|\d{2}(?:\/\d{2}\/\d{4})?)\b/;
[
  "the response value 10",
  "the response value 10/12/2014",
  "the response value 08/2015"
].forEach(s => console.log(s.match(pattern)[0]));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Creating regex to extract 4 digit number from string using java

分類Dev

Regex to extract date and specific string

分類Dev

Extract number from string

分類Dev

Regex to extract date from text

分類Dev

Extract data from string with regex

分類Dev

Extract maximum number from a string

分類Dev

Extract phone number from String

分類Dev

How to extract phone number & email id from a string using Python Regex

分類Dev

Regex to extract number from string (gwmi Win32_OperatingSystem).Name

分類Dev

Extract Floats from a String - Regex - Python

分類Dev

Extract values from string using regex groups

分類Dev

C++ Extract number from the middle of a string

分類Dev

Extract number from string in python without re

分類Dev

How to extract number from character string in R?

分類Dev

How to extract string from dataframe after regex matching

分類Dev

Extract string from a line of SPSS syntax and convert to date

分類Dev

Extract number after $ sign regex

分類Dev

how to extract string or number from bs4.element.NavigableString

分類Dev

JS: Extract number from string, that is before a specific word

分類Dev

Extract number of length n from field and return string

分類Dev

Excel: Extract a project number from a text string (various position and length)

分類Dev

extract number after specific string

分類Dev

Extract from string in Java

分類Dev

Extract tensor from string

分類Dev

Extract dict from string

分類Dev

regex to extract link from url

分類Dev

Javascript Extract from substring regex

分類Dev

REGEX: Extract info from URL

分類Dev

Extract date and time from ID

Related 関連記事

ホットタグ

アーカイブ