0

I have not been able to find a proper regex to match any string not starting and ending with some condition.

This matches

AS.E
23.5
3.45

This doesn't match

.263
321.
.ASD

The regex can be alpha-numeric character with optional '.' character and it has to be with in range of 2-4(minimum 2 chars & maximum 4 chars).

I was able to create one ->

^[^\.][A-Z|0-9|\.]{2,4}$

but with this I couldn't achieve mask '.' character at the end of regex.

Thanks.

7
  • You don't need pipes (|) inside brackets ([]). You should use a tool to help you build your regex, like regex101.com Commented Jul 2, 2018 at 14:31
  • 1
    Try ^(?=.{2,4}$)[A-Z0-9]+\.[A-Z0-9]+$ Commented Jul 2, 2018 at 14:35
  • Thanks Wiktor Stribiżew. Your regex helped me. Commented Jul 2, 2018 at 14:38
  • 1
    Shouldn't just this suffice ^[A-Z0-9]{1,2}\.[A-Z0-9]{1,2}? I don't get why you need lookahead? Commented Jul 2, 2018 at 14:40
  • 1
    I think it should be ^(?=.{2,4}$)[A-Z0-9]+\.?[A-Z0-9]+$. Sam, is that so? Commented Jul 2, 2018 at 14:42

3 Answers 3

2

Maybe not the most optimized but a working one. Created step by step:

  1. The first character should be alphanumeric

    ^[a-zA-Z0-9]

  2. 0, 1 or 2 character alphanumeric or . but not matching end of string

    [a-zA-Z0-9\.]{0,2}

  3. an alphanumeric character matching end of string

    [a-zA-Z0-9]$

  4. Concatenate all of this to obtain your regex

    ^[a-zA-Z0-9][a-zA-Z0-9\.]{0,2}[a-zA-Z0-9]$

Edit: This regex allows multiple dots (up to 2)

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

3 Comments

I think OP only allows 1 dot per string. See with optional '.' character.
@WiktorStribiżew well seen ! It complicates things... I'll try to edit to complete this
The solution is here. Else, you need to spell out all alternatives.
1

If I guessed correctly, you want to match all words that are

  • Between 2 and 4 characters long ...
  • ... and start and end with a character from [A-Z0-9] ...
  • ... and have characters from [A-Z0-9.] in the middle ...
  • ... and are not preceded or followed by a ..

Try this regex to match all these substrings in a text:

(?<=^|[^.])[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9](?=$|[^.])

However, note that this will match the AA in .AAAA.. If you don't want this match, then please give more details on your requirements.

When you are only interested in the number of matches, but not the matched strings, then you could use

(^|[^.])[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9]($|[^.])

If you have one string, and want to know whether that string completely matches or not, then use

^[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9]$

If there may be at most one . inside the match, replace the part [A-Z0-9.]{0,2} with ([A-Z0-9]?[A-Z0-9.]?|[A-Z0-9.]?[A-Z0-9]?).

Comments

0

You can use this pattern to match what you say,

^[^\.][a-zA-Z0-9\.]{2,4}[^\.]$

Check the result here.. https://regex101.com/r/8BNdDg/3

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.