Regex Match Digits Between Strings

Regex match digits between strings

You may use

std::regex reg(R"(start:\s*(\d+)\s*end:)");

See the regex demo.

It defines the start:\s*(\d+)\s*end: regex pattern that matches start:, 0+ whitespaces, then captures into Group 1 one or more digits, and then matches 0+ whitespaces and end: substring.

Note that in case you cannot use raw string literals (R"(...)" notation), you may define the pattern with a regular string literal where all backslashes should be doubled: "start:\\s*(\\d+)\\s*end:".

To obtain all matches, you need std::sregex_token_iterator and when getting the matches, specify that you need to grab all Group 1 values:

const std::regex reg(R"(start:\s*(\d+)\s*end:)");
std::smatch match;
std::string s = "garbage 111222 garbage ... 999888 fewfew... start: 123456 end: start: 654321 end:";
std::vector<std::string> results(std::sregex_token_iterator(s.begin(), s.end(), reg, 1),
std::sregex_token_iterator());

See the online C++ demo

If there can be any value inside start: and end:, replace \d+ with .*? (matching any 0+ chars other than line break characters).

Regex Match all characters between two strings

For example

(?<=This is)(.*)(?=sentence)

Regexr

I used lookbehind (?<=) and look ahead (?=) so that "This is" and "sentence" is not included in the match, but this is up to your use case, you can also simply write This is(.*)sentence.

The important thing here is that you activate the "dotall" mode of your regex engine, so that the . is matching the newline. But how you do this depends on your regex engine.

The next thing is if you use .* or .*?. The first one is greedy and will match till the last "sentence" in your string, the second one is lazy and will match till the next "sentence" in your string.

Update

Regexr

This is(?s)(.*)sentence

Where the (?s) turns on the dotall modifier, making the . matching the newline characters.

Update 2:

(?<=is \()(.*?)(?=\s*\))

is matching your example "This is (a simple) sentence". See here on Regexr

Regex Python extract digits between two strings in a single expression

You can try like this

import re
text = "Advance [Extra Value of $1,730,555] in packages 2,3, and 5."
match = re.findall(r'\$(.*)]',text)[0].replace(',','')
print match

Regex Match in between Strings

You can use String.match(Regex)

"9978434276K12345678".match(/[a-zA-Z][0-9]{8}/)

It returns an array of 4 elements: [String coincidence, index: Number, input: String, groups: undefined]

just stay with the element 0: coincidence and 1: index of the match.

and use this just to check that the string matches at least one

/Regex/.test(String)
/[a-zA-Z][0-9]{8}/.test("9978434276K12345678")

It will return true or false

USE expression without quotation marks

const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);

Regex match two strings with given number of words in between strings

You can use something like

import re
text = 'I want apples and oranges'
k = 2
pattern = f"apples(?:\s+\w+){{0,{k}}}\s+oranges"
m = re.search(pattern, text)
if m:
print(m.group())

# => apples and oranges

Here, I used \w+ to match a word. If the word is a non-whitespace chunk, you need to use

pattern = f"apples(?:\s+\S+){{0,{k}}}\s+oranges"

See this Python demo.

If you need to add word boundaries, you need to study the Word boundary with words starting or ending with special characters gives unexpected results and Match a whole word in a string using dynamic regex posts. For the current example, fr"\bapples(?:\s+\w+){{0,{k}}}\s+oranges\b" will work.

The pattern will look like apples(?:\s+\w+){0,k}\s+oranges and match

  • apples - an apples string
  • (?:\s+\w+){0,k} - zero to k repetitions of one or more whitespaces and one or more word chars
  • \s+ - one or more whitespaces
  • oranges an oranges string.

Regex to catch strings with only 2 digits

You could use ^\D*(\d)\D*(\d)\D*$

\D* makes sure anything before your first digit isn't another digit.

(\d) captures your first digit.

\D* makes sure there is no other digits between your two digits.

(\d) captures your second digit.

\D* makes sure anything after your second digit isn't another digit.

RegEx Capture First String between Two Numbers

If you want to extract the first match, you could start with an anchor ^ matching any char except a digit \D* and then match a digit with an optional decimal part.

^\D*\d+(?:[.,]\d+)*(\D+)\d
  • ^ Start of string
  • \D* Match 0+ times any char except a digit
  • \d+(?:[.,]\d+)* Match 1+ digits and optionally repeat a . or , and 1+ digits
  • (\D+) Capture group 1, match 1+ times any char except a digit
  • \d Match a digit

Regex demo

To prevent crossing newline boundaries:

^[^\d\n\r]*\d+(?:[,.]\d+)*([^\d\n\r]+)\d

Regex demo

How to read first set of numbers between strings using regex?

You want to get a streak of digits in between two letters.

You can use

(?<=[a-zA-Z])\d+(?=[a-zA-Z])

See the .NET regex demo.

Or, if you want to get the digits after the leading non-digit chars, use

(?<=^\D+)\d+(?=[a-zA-Z])

See this .NET regex demo.

In C#, you can use Regex.Match:

var result = Regex.Match(text, @"(?<=^\D+)\d+(?=[a-zA-Z])")?.Value;

Regex details:

  • (?<=[a-zA-Z]) - right before the current location, there must be an ASCII letter (use \p{L} to match any letter)
  • (?<=^\D+) - right before the current location, there must be start of string + any one or more non-digit chars (use \D* if the digits can appear at the start of string)
  • \d+ - one or more digits
  • (?=[a-zA-Z]) - right after the current location, there must be an ASCII letter (use \p{L} to match any letter).

Regex match digits between two strings not working in JS

JavaScript doesn't support positive or negative lookbehind. But, you might want to try to capture its group and use them as you're replacing the string.

E.g

var str = 'http://www.domain.com/foo/1234/bar/';
var myvar = 'newnumber';
var newStr = str.replace(/(foo\/)(\d+)(\/bar)/i, '$1' + myvar + '$3');
// Returns "http://www.domain.com/foo/newnumber/bar/"
  • (foo\/) is the first group, matching foo/
  • (\d+) is the second group, matching any digits number for one or more.
  • (\/bar) is the third group, matching /bar
  • '$1'+ myvar +'$3' returns a concat of first group + myvar + third group

Extract only digits between two specific tags using regex

Since you specified the numbers are always at the end of the string inside the tag (in the comments below your question), you don't need to ensure the opening tag is there. Just match before the closing tag: You're not validating, but extracting.

See regex in use here

\d+(?=<\/Guid>)

If you must ensure the start is there, you may use <Guid>.*?\K\d+(?=<\/Guid>) instead. \K resets the starting point of the reported match.



Related Topics



Leave a reply



Submit