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.

sparker
  • 1,666
  • 4
  • 21
  • 35
  • Possible duplicate of [Is it possible for a computer to "learn" a regular expression by user-provided examples?](https://stackoverflow.com/questions/616292/is-it-possible-for-a-computer-to-learn-a-regular-expression-by-user-provided-e) – Venki WAR May 18 '18 at 03: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]+` – Andreas May 18 '18 at 04:34
  • @Andreas exactly. Thanks.. – sparker May 18 '18 at 04:36
  • The regex could be *shortened* by specifying the [case-insensitive](https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#CASE_INSENSITIVE) flag: `(?i)[a-z]+:[a-z0-9]+:[a-z]+:[0-9]+` – Andreas May 18 '18 at 04:39

6 Answers6

2

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

xingbin
  • 27,410
  • 9
  • 53
  • 103
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]+

Gianluca Mereu
  • 300
  • 1
  • 4
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!).

ikramf90
  • 92
  • 2
  • 17
0

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

Venki WAR
  • 1,997
  • 4
  • 25
  • 38
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

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
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+)
Laurent Mouchart
  • 216
  • 1
  • 3
  • 13
  • What is the benefit of capturing the whole regex in a capturing group? Your regex [`([A-z]*:(?:[A-z]|\d)*:[A-z]*:\d*)`](https://regex101.com/r/R3V2J5/1) also matches `_:_:_:` , `:::` or `[\]^_`:_:_:` because `[a-Z]` [matches more than you think](https://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret). 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 `:` – The fourth bird May 18 '18 at 13:28
  • [`([A-z]*:(?:[A-z]|\d)*:[A-z]*:\d*)`](https://regex101.com/r/R3V2J5/1) does match `_:_:_:` because the `\d*` matches zero or more times. You can check the link. – The fourth bird May 18 '18 at 13:41