1

I want to validate this string "B3:D7,0 H3:J7,0 B9:L27,1 B29:L48,2 B50:L56,2" using pattern but it doesn't match. I don't know what error there is in the regex string.

I used pattern="[A-Z][0-99]+:[A-Z][0-99]+,[0-9]+" inside textfield in html

2
  • FYI, [0-99] is the same as [0-9]. Commented May 4, 2021 at 12:13
  • If you're new to regex, you can take a look at regexr.com where you can paste your regular expression and it will explain to you in English what it's trying to match. Commented May 4, 2021 at 12:14

3 Answers 3

1

Forgot ( and )+ around your pattern:

pattern="([A-Z][0-9]+:[A-Z][0-9]+,[0-9]+)+"

or

pattern="([A-Z][0-9]+:[A-Z][0-9]+,[0-9])+"

if the last digit may occour only once.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try the below pattern:

([A-Z][0-9]+\:[A-Z][0-9]+,[0-9]\s?){5}

Comments

0

To match a digit 0-9 you can use [0-9] or \d

Assuming you don't want spaces at the start and at the end, you can repeat the pattern 1 or more times preceded by a space.

^[A-Z]\d+:[A-Z]\d+,\d+(?: [A-Z]\d+:[A-Z]\d+,\d+)+$
  • ^ Start of string
  • [A-Z]\d+:[A-Z]\d+,\d+ Match : between a char A-Z and 1+ digits followed by , and 1+ digits
  • (?: [A-Z]\d+:[A-Z]\d+,\d+)+ Repeat 1+ times the same pattern with a space preceded.
  • $ End of string

Regex demo

Using the pattern attribute, you can omit the start and end anchors as they are already implied.

pattern="[A-Z]\d+:[A-Z]\d+,\d+(?: [A-Z]\d+:[A-Z]\d+,\d+)+"

Note that if you want to match a digit 1-99 you can use [1-9]\d?

2 Comments

Works for "B3:D7,0 H3:J7,0 B9:L27,1 B29:L48,2 B50:L56,2" but doesn't work for string like "A1:X30,2"
@HemilShah Then you can use ^[A-Z]\d+:[A-Z]\d+,\d+(?: [A-Z]\d+:[A-Z]\d+,\d+)*$ with * as a quantifier for 0 or more times. See regex101.com/r/sMjQWJ/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.