Regular expression for format XX-XXXX - regex

I want a regular expression for format xx-xxxx can somebody help? It will take numbers and
characters. I have no idea about regular expressions. also can some explain that how whole expression works?
Thanks

Assuming that the string will be only containing exactly that string with no leading or trailing whitespace:
/^[a-zA-Z0-9]{2}-[a-zA-Z0-9]{4}$/
If you need to find that pattern within another string (a paragraph?) you would use:
/\b[a-zA-Z0-9]{2}-[a-zA-Z0-9]{4}\b/

The expression:
\A[0-9a-zA-Z]{2}-[0-9a-zA-Z]{4}\Z
will also work.

Related

Regular expression - How to exclude certain string from the results set?

I am pretty new in Regular Expression.
Input
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
/shop/necklaces
/shop/yellow-gold-necklaces
/shop/white-gold-necklaces
/shop/rose-gold-necklaces
/shop/best-buy-earrings
Regular Expression I used
\/shop\/[a-z-]*-?earrings
Desired Result
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
Actual Result
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
/shop/best-buy-earrings
I do not want /shop/best-buy-earrings to be in the result. Please help me to fix the Regular Expression. Thank you.
Simply add gold to the regex before the - and surround with parenthesis:
\/shop\/([a-z-]*gold-)?earrings
Assuming PCRE flavour, you can use:
\/shop\/(?!best-buy)(?:\w+-)*earrings
Or, if you can use other delimiter than slash:
/shop/(?!best-buy)(?:\w+-)*earrings
Demo

Regular Expression Pattern which should not allow ,;:|

I need the regular expression pattern which should not allow to put any of following characters into input in HTML
,;:|
You may get some idea from this.
[^,;:|]+
DEMO::: https://rubular.com/r/dKQzC1HrnMG88X

regular expression ~ extract string

I need to extract using Regular Expression from following string
console.log("This can be anything except double quote"),
followed by comma and any other string
and the extraction output is
console.log("This can be anything except double quote"),
Note that the sample string shall not be read literally (e.g. can be anything means a random string or symbol
~!##$%^&*)
Any idea, what is the right regular expression for above case?
Using Regex for quoted string with escaping quotes:
(console\.log\("(?:[^"\\]|\\.)*"\),)
There are many solutions to this.
The simplest I could think of: (console.log[^,]+)
PS: This removes the comma at the end of the console statement. You can manually add that.

Regular Expression Time Format?

I have a regular expression which accept time in a specific format like the following,
"10:00".
I want to change the regular expression to etiher accpet this format or accept only a single one dash only ("-").
Here is the expression:
/^((\d)|(0\d)|(1\d)|(2[0-3]))\:((\d)|([0-5]\d))$/
Key points to solving this:
Square brackets ([ and ]) are used to enclose character classes.
The pipe | means or.
[\:|-] means chech for either a literal : or a hyphen -.
The resulting pattern is:
^((\d)|(0\d)|(1\d)|(2[0-3]))[\:|-]((\d)|([0-5]\d))$
Just use an alternative:
^<your regex>$|^-$
This will match either a time in your format or a single hyphen-minus.
Does this regex need to have so many brackets?
/^(([01]?\d|2[0-3]):[0-5]\d|-)$/

Validate certain string using RegEx

I need to validate following string using Regular Expressions.
These are the constraints which apply for this string.
From beggining it has 9 numbers.
In the end it has character 'V' or 'X'. They can be simple or capital.
Whole length of string must be 10.
Ex: 84256142V, 547812375X
Can anyone provide me RegEx for validate this.
^\d{9}[VX]$ if you put the regex engine in case-insensitive mode, ^\d{9}[vVxX]$ otherwise.
Depends on the language, but it will be something like this:
^[0-9]{9}[VvXx]$
This is the Regex you need: /^(\d){9}(V|X)$/i