Regex to match series and numbers starting with predefined characters - regex

Is it possible to match just the numbers from a predefined set of characters looking like:
FACTURA SBG113151
What I want: 113151
What I'm trying: FACTURA[ ]*([a-z0-9A-Z]+)
What I get: SBG113151
Any help is much appreciated! Thanks.

You can use
FACTURA *[a-zA-Z]*([0-9]+)
FACTURA[^0-9]*([0-9]+)
See the regex demo. The point is to match and consume the chars before the digit sequence you need to extract.
Details:
FACTURA - a word
* - zero or more spaces
[a-zA-Z]* - zero or more letters
[^0-9]* - in the other regex, matches zero or more chars other than digits
([0-9]+) - Group 1: one or more digits

Related

What is the Regex pattern "words with numbers in them" but not a number itself

How does a regex look like for
Input:
Rood Li-Ion 12 G6
Match:
"Rood" "Li-Ion" "G6"
1.
I tried
\b[\w-]+\b /g
But that matches the "12" also!
2.I tried
/([0-9]+)?[a-zA-ZĂȘ]/
But that didn't match G6.
I want all words even if they have a number in them but I dont want only numbers to match. How is this possible. Whitespace also shall not be part of the match.
"Rood Li-Ion 12 G6" shall become 3 strings of "Rood","Li-Ion","G6"
You can use
(?<!\S)(?!\d+(?!\S))\w+(?:-\w+)*(?!\S)
See the regex demo. It matches strings between whitespaces or start/end of string, and only when this non-whitespace chunk is not a digit only chunk.
Also, it won't match a streak of hyphens as your original regex.
Details
(?<!\S) - a left whitespace boundary
(?!\d+(?!\S)) - no one or more digits immediately to the right capped with whitespace or end of string is allowed
\w+(?:-\w+)* - one or more word chars followed with zero or more repetitions of - and one or more word chars
(?!\S) - a right whitespace boundary
This should suit your needs:
\b[\w-]*[a-zA-Z][\w-]*\b

Working with regex for alphanumeric

I'm trying a regex fro Alpha Numeric of length 7 (with positions 1,3,4 as characters and positions 2,5,6,7 as digits).
[a-zA-Z]|[0-9]|[a-zA-Z]|[a-zA-Z]|[0-9]|[0-9]|[0-9]
Can someone help me?
The sequence "character, digit, character, character, digit, digit, digit" is expressed in regex as
[a-zA-Z][0-9][a-zA-Z]{2}[0-9]{3}
If you're working in PCRE (with say, PHP):
^([a-zA-Z])([0-9])(?1){2}(?2){3}$
Breakdown:
^ - from the start of the string
([a-zA-Z]) - match and capture a single character in the ranges given: a-z, A-Z
([0-9]) - match and capture a single character in the ranges given: 0-9
(?1){2} - redo the regex in the first group twice (recursive subpattern)
(?2){3} - redo the regex in the second group 3 times (recursive subpattern)
$ - the end of the string
If you want to match this in the middle of a sentence, exchange ^ and $ for \b - which will match a word boundary
See the demo
If you're not using PCRE:
^[a-zA-Z][0-9][a-zA-Z]{2}[0-9]{3}$
Which does the same thing, but has some copy-paste involved

How do you complete the following Regex

I am looking for a regex expression that will accept the following: The capital letter A followed by any number of digits. This might also be a decimal number. All of these are valid: A1, A500, A543.987
This is NOT OK to accept: Apple, AE100
Currently I have [A]\w.[0-9]* but it accepts App and AE100.
You may use the following regex if the entire string should match:
^A[0-9]+(?:\.[0-9]+)?$
Or, to match these strings as whole words:
\bA[0-9]+(?:\.[0-9]+)?\b
See the regex demo.
Details
^ - start of string / \b - a word boundary
A - an A
[0-9]+ - 1+ digits
(?:\.[0-9]+)? - an optional sequence of . and then 1+ digits
$ - end of string / \b - a word boundary.
I would suggest "A\d+(\.\d+)?". \d represents all digits, the + is one or more characters and the (\.\d+)? is a . followed by one or more digits. But the ? specifies it's optional.

Regex: find all strings which include at least 3 numeric chars and that there's at least one space between them, how can it be done?

I have a regex that matches strings with alphanumeric chars which are 12 chars and longer:
/\b(?=[a-zA-Z0-9]{12,}).*\d+.*\b/g
I want it to return matches with only more than 2 digits and at least one of them must be separated than the other 2.
Examples:
abcdefghijk9lmn8 - no match
abcdefghijklmn987 - no match
abcdefg9hijklmn87 - match
abcdefg9hijkl8mn7 - match
Can you please help me with that regex?
You can use this regex with 2 lookaheads:
^(?=(?:[a-zA-Z]*\d){3})(?=.*\d[a-zA-Z]+\d)[0-9A-Za-z]{12,}$
RegEx Demo
(?=(?:[a-zA-Z]*\d){3}) - Lookahead to assert at least 3 digits in input
(?=.*\d[a-zA-Z]+\d) - Lookahead to assert that at least 2 digits have an alphabet in between

Regex Match for Valid Dates only

I am trying to create a regex that only matches for valid dates (in MM/DD or MM/DD/YY(YY) format)
My current regex (\d+)/(\d+)/?(\d+)? is very simple but it matches any number that has a / before/after. I.e. if a string is 2015/2016 12/25 it will see both of these as matches but i only want the 12/25 portion.
Here is a link to some sample RegEx.
You can add word boundaries (\b) to make sure you match the date string as a whole "word" (so that the match does not start in the middle of a number) and restrict the occurrences \d matches with the help of limiting quantifiers:
\b(\d{2})/(\d{1,2})/?(\d{4}|\d{2})?\b
See the regex demo
The regex breakdown:
\b - word boundary to make sure there is a non-word character or start of string right before the digit
(\d{2}) - match exactly 2 digits
/ - match a literal /
(\d{1,2}) - match and capture 1 to 2 digits
/? - match 1 or 0 /
(\d{4}|\d{2})? - match 1 or 0 occurrences of either 4 or 2 digits
\b - trailing word boundary