1

I'm having trouble writing a regex that matches a pattern like this "%n%m%p" or "%n:%m%p". Only allow specific letters and each letter must have percent sign in front of it. No numbers allowed.

This regex /%(n|m|p)$/ works but allows numbers in between. For example this "%n3%p%m" matches. How do I disallow any numbers.

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
Eric
  • 175
  • 10
  • 1
    it should match the whole sequence or any "proper" character ? For this input `%n3%p%m` - should it match `%p%m` ? Elaborate your question – RomanPerekhrest Dec 04 '16 at 16:56
  • The regex `%(n|m|p)` itself matches either `%n` or `%m` or `%p`. That the numbers are allowed between each of the parts is most likely because of your other code. – NtFreX Dec 04 '16 at 16:56
  • @RomanPerekhrest Yes it matches `%p%m` That is how I want it to work. As long as it has one of the letters and a percent sign it should match. Just no numbers. – Eric Dec 04 '16 at 17:04

4 Answers4

1

The regex %(n|m|p) itself matches either %n or %m or %p. That the numbers are allowed between each of the parts is most likely because of your other code.

You can match the whole with this regex

/^(%(n|m|p):{0,1}){0,}$/
NtFreX
  • 10,379
  • 2
  • 43
  • 63
1

Just need to be clear about the exact requirements.

  1. The allowed letters are [nmp]
  2. Each letter has to be preceded by a %
  3. There can be an optional : before %
  4. + One or more tokens from ^ start to $ end

These requirements won't allow any digit.

^(?::?%[nmp])+$

You can test it at regex101

Community
  • 1
  • 1
bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • Thanks this works but what if wanted to make it more flexible and allow more than one non-alphanumeric character other than a colon `:` For example how would I do this `"%n.%m"` or `"%n_%m" – Eric Dec 04 '16 at 18:04
  • 1
    @Eric Put them into a [character class](http://www.regular-expressions.info/charclass.html), [see this demo](https://www.regex101.com/r/EZNTsM/1). – bobble bubble Dec 04 '16 at 18:07
0

I can't leave a comment but I can answer, so...

It would help to know what exactly you need from this. Do you need those letters in that order? Do you need exactly 3? Or are you looking for any number of any length with any valid characters in between?

That said, one option if you're matching the entire string is

/^(%[nmp][^\d]*)+$/

which should match any %[nmp] with any character between them that isn't a number. Note though that this will match a single %n for example. If you want to match a specific number i or more than a certain number j, change the + to {i} or {j,} respectively.

Iluvatar
  • 1,537
  • 12
  • 13
  • Thanks for the reply but this doesn't work. A percent sign must be in front of each letter. `%nm` will match. It should be `%n%m` – Eric Dec 04 '16 at 17:19
  • `%nm` will match the `%n` part. Assuming you're matching the whole string then, you could use `/^(%[nmp][^\d]*)+%[nmp]$/`. If Dr. Fre's answer is the one that works for you, then that works, but note it also matches `%n:`. – Iluvatar Dec 04 '16 at 17:34
0

As long as it has one of the letters and a percent sign it should match. Just no numbers

Use the following regex pattern:

%[nmp](?!\d)\b

https://regex101.com/r/CrSnFp/2

(?!\d) - negative lookahead assertion, matches one of the specified characters if it's not followed by a number

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Thanks but this one doesn't work either. Maybe I wasn't clear enough. If a percent sign is left off a letter it still matches. I don't want this to match `"%nm"` Each letter must have a percent `"%n%m"` – Eric Dec 04 '16 at 17:26