This question already has answers here:
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 2 years ago.
I know there are similar question asked (might the same question here) but I can't seems to get my regex to work. I tried the answer there but didn't work which is below.
^[A-Z]{2}\d+
I'm trying to check if the country serial or organisation numbers start with specific two capital letters. For example BE0462427110 is from a Belgium company so all the org. numbers should start with BE. German once have DE and Italian once have IT in front.
So I tried below my own version as well.
^([BE]{2})([\d]{6,10})$
Below is a test block I use to extract the numbers from using regex. And the number is after "BEPC0 PARTS".
C0MMUNAUTE EUR0PEENNE 1 ExpCd1teur/Exp0rtateur (2) N0. BEPC0 PARTS
BE0462427110 Rue Chaum0nt(Herm) 4 D F BE 4480 Eng1s E D q 1jj -
Dest1na1re (8) N0, N M BUCHER 1ANDTECHN1K SA 7
After the above regex didn't work I tried another.
^([B][E])([\d]{6,10})$
So what am I doing wrong here ?
This anchors at the beginning and end of the string:
^([A-Z]{2})([\d]{6,10})$
You are looking for a pattern somewhere within a string, so you can anchor by word boundaries \b:
\b([A-Z]{2})([\d]{6,10})\b
Related
This question already has answers here:
RegEx BackReference to Match Different Values
(2 answers)
Closed 2 years ago.
Consider the following strings:
hotpoint nm11 823 wk eu n 1200 rpm 8 kg class a+++
hotpoint nm11 823 wk eu n rpm 1200 8 kg class a+++
hotpoint nm11 823 wk eu n rpm1200 8 kg class a+++
How could I write a regexp, that will match the highlighted part in either string? Basically, I want a regexp that will match part of a string that either starts with rpm or ends with rpm and contains only spaces and numbers in between.
To do a regexp that starts with rpm and contains only numbers or spaces, I could do something like this:
/((rpm)\s{0,1}(\d+))/
and I also know that I can achieve the desired result by adding an additional | (OR) part to the regexp by this:
/((rpm)\s{0,1}(\d+)|(\d+)\s{0,1}rpm)/
What I am interested in, if there is any other way to specify that the matched part should either start or end with something specific, without duplicating the whole matching rule?
Note, that the part of the string I am looking for is not on the end or the beginning of the string, so I can't use $ or ^
In some regex flavors, including PCRE and PyPi in Python, if you put a capture group around the pattern, you can match the pattern again elsewhere with (?1) syntax, with the 1 replaced with the capture group index, like this:
(rpm)\s?\d+|\d+\s?(?1)
https://regex101.com/r/neK7cC/1
Note that {0,1} simplifies to ?.
If one isn't lucky enough to be working with an engine that supports recursing subpatterns, one will have to repeat the pattern again, or find out another way.
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 2 years ago.
Fairly new to regex, looking to identify a user id pattern that is all alpha, is exactly 6 characters, and the second character is always a Z. Any help would be appreciated.
How about this regex:
\w(Z|z)\w{4}
Is this what you want?
As I understood you want something to detect something like this:
jZabcd
What you could do is something like this:
[A-Za-z][Z]([A-Za-z]{4})
Breakdown:
[A-Za-z] = detects all alpha big and small letters only once.
[Z] = checks if there is only a big "Z".
() = a group to make it easier.
{4} = checks if there is 4 times from what was infront of the 4.
[A-Za-z]{4} = checks if there are 4 letters from big A-Z and small a-z.
I hope this helps you out and please expand your question next time a little more.
This question already has answers here:
Comma Separated Numbers Regex
(6 answers)
Closed 3 years ago.
I have the requirement to restrict a non-required textbox to only numbers (0-9) with a separator of ';'. The pattern is that the groups can be 4 or 5 in length and can repeat n times. Anything less than 4 in a group is invalid. After 4 or 5 I need to check for the existence of a separator character ';'. This pattern can repeat n times. I have tried variations of but this doesn't seem to be working. Something simple to start out like
[0-9]{4,5};+
is invalid as I don't need the separator for only 1 number grouping.
Next I tried
^[0-9]{4,5}|[0-9]{4,4};|[0-9]{5,5};$
but this doesn't work because the existence of four digits like 1111 or five digits 11111 before gives one match before it goes awry example "11111;j" Is there a way in a regex to validate
1111
11111
1111;1111
11111;1111
11111;11111
but catch
111
111;
1111;1
11111;1
abc
in a repeating fashion?
This validate your examples.
^[0-9]{4,5}(;[0-9]{4,5})?$
Try it
It's not clare what you mean by "in a repeating fashion". If you want validate also this
1111;11111;11111;1111;11111
You can use this regex
^[0-9]{4,5}(;[0-9]{4,5})*$
Try it
This question already has answers here:
How can I match "anything up until this sequence of characters" in a regular expression?
(15 answers)
Closed 3 years ago.
I have strings that are concatenations of airline codes/flightnumbers, separated with ;. The airline code and flight number are either separated by a space or -. So some examples are:
AA-1234;UA 243;EK 23;
9W 23;B6-134
Now I want to grab the airline codes from this.
I came up with the following regex: [a-zA-Z0-9]{2}[ -]. This works to grab the airline codes but also includes the airlinecode-flightnumber separator. How would I adjust my regex to not include this?
[a-zA-Z0-9]{2}(?=[ -])
See it in action here
This question already has answers here:
What's a Regex pattern for 3 consecutive digits increasing or decreasing
(3 answers)
Closed 6 years ago.
i would like to ask if it is possible to get a regex to check for subsequent alphabets or numbers in a string such as "abcd" or "1234" 4 characters in succession.
This is for password validation to check if a user is trying to enter a password like "abcd1234"
Thank You
Based on the answer Match increasing/decreasing sequences using regex, this pattern does close. Through programming you still need to get the first capturing group \1 and check whether its size is equal to 4. Here you need to set the multi line mode, to allow $ match the end of line.
This is because this regex is capturing all possible groups, however the groups you want has the length of 4, so it should not be a problem.
(?x)
(
(?:a(?=b|$))?
(?:b(?=c|$))?
(?:c(?=d|$))?
(?:d(?=e|$))?
(?:e(?=f|$))?
(?:f(?=g|$))?
(?:g(?=h|$))?
(?:h(?=i|$))?
(?:i(?=j|$))?
(?:j(?=k|$))?
(?:k(?=l|$))?
(?:l(?=m|$))?
(?:m(?=n|$))?
(?:n(?=o|$))?
(?:o(?=p|$))?
(?:p(?=q|$))?
(?:q(?=r|$))?
(?:r(?=s|$))?
(?:s(?=t|$))?
(?:t(?=u|$))?
(?:u(?=x|$))?
(?:x(?=z|$))?
[a-z]?
|
(?:0(?=1|$))?
(?:1(?=2|$))?
(?:2(?=3|$))?
(?:3(?=4|$))?
(?:4(?=5|$))?
(?:5(?=6|$))?
(?:6(?=7|$))?
(?:7(?=8|$))?
(?:8(?=9|$))?
\d?
)
On this link you can see the live regex: https://regex101.com/r/xxED4s/2