1

I need to generate a regExr for the below String pattern.

A:B:C:D

where A should contain only Strings and B can contain both String and a digits and C should be a String and D should only be the digits.

I created RegExr myself and its working.

[a-zA-Z]+(:)+[a-zA-Z1-9]+(:)+[a-zA-Z]+(:)+[1-9]+

But is there a better regExr for the pattern I want something bit shorter than mine? Or mine is ok?

I am using Java8.

4
  • Possible duplicate of Is it possible for a computer to "learn" a regular expression by user-provided examples? Commented May 18, 2018 at 3:51
  • 1
    You do know that (:)+ means one-or-more colons, right? Seems you want exactly one colon, and you don't need to capture it. Also, a 0 is a digit too, you know. So this is what you need: [a-zA-Z]+:[a-zA-Z0-9]+:[a-zA-Z]+:[0-9]+ Commented May 18, 2018 at 4:34
  • @Andreas exactly. Thanks.. Commented May 18, 2018 at 4:36
  • The regex could be shortened by specifying the case-insensitive flag: (?i)[a-z]+:[a-z0-9]+:[a-z]+:[0-9]+ Commented May 18, 2018 at 4:39

6 Answers 6

2

Use \d instead 1-9(if 0 is permitted).

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

Comments

2

You can use \d for digits and if you don't mind accepting the underscore you can also use the \w for the [a-zA-Z].

Your Regexp could become:

[\w]+:[\w\d]+:[\w]+:[\d]+

1 Comment

Thanks. There shouldn't be any underscore.
1

Your regex code looks completely fine if you have to avoid 0 and _ otherwise you can use above examples. If you want to pass it as parameter then try assinging it to a string, that will be more convinient(which I haven't tried yet!).

1 Comment

What "above examples"?
0

Your RegEx only captures the :.

This RegEx works for "Hello:hel35a:ahelkj:684579" and captures the whole String.

 ([A-Za-z]+:(?:[A-Za-z]|\d)+:[A-Za-z]+:\d+)

2 Comments

What is the benefit of capturing the whole regex in a capturing group? Your regex ([A-z]*:(?:[A-z]|\d)*:[A-z]*:\d*) also matches _:_:_: , ::: or [\]^_:_:_:` because [a-Z] matches more than you think. The OP commented that it should not match underscores. Using the asterix * means repeat zero or more times. That means that you made everything optional except the colons :
([A-z]*:(?:[A-z]|\d)*:[A-z]*:\d*) does match _:_:_: because the \d* matches zero or more times. You can check the link.
0

You Expression looking good. If you want more regEx operations use this site it is very helpful txt2re

1 Comment

Regex is not good, since it would match e.g. A:::B:C:D, and it wouldn't match A:B:C:0
0

You might use a word boundary \b if you don't' want your match to be part of a larger match. (Or use anchors ^ and $ to assert the start and end of the string)

For example a:1a:b:0 would match, but a:1a:b:0a does not because it has an a at the end.

To match a digit 0-9 you could use \d and to match a single colon you could just use : without a quantifier + or a capturing group ()

Your regex could look like this:

note the case insensitive flag /i in the demo to use [a-z] instead of [A-Za-z]

\b[a-z]+:[a-z\d]+:[a-z]+:\d+\b

Java:

\\b[a-z]+:[a-z\\d]+:[a-z]+:\\d+\\b

Comments

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.