Alternative shorter regex


I have this string:

11ffff' hh'h777 hjhh'6565 666 ggèéçàùgg rrt'rt

and I have found on my own this regex that matches that string:

/^[^ \']((\d+\pL+[\']?|\pL+\d+|\pL+[\']?\d+|\d+[ ]?\pL+[\']?|\pL+[\']?)[ ]?)+$/u

you can see it on

it goes good, but since I’m new with regex, I wonder if there exist an alternative regex maybe more efficient or well written. thanks!

all your one character character classes can be reduced to the character itself.

can you post any example?

A one character character class would be like
which could be written as

ok, I think I understand. So this would be correct:

/^[^ \']((\d+\pL+\'?|\pL+\d+|\pL+\'?\d+|\d+[ ]?\pL+\'?|\pL+\'?)[ ]?)+$/u

is there any other optimization that it is possible to do?

If you analyze the “ors” in the capture is that any help?

\d+\pL+\'?	11ffff' 
\pL+\d+		h777 
\pL+\'?\d+	h777	hjhh'6565 	
\d+[ ]?\pL+\'?	11ffff' 777 hjhh'	666 ggèéçàùgg	
\pL+\'?		ffff' 	hh'		h		hjhh'	ggèéçàùgg 	rrt'	rt

I’m so sorry, I don’t understand, please can you explain me better?

The left column is the expression fragments.

I didn’t test in regex101 or elsewhere, so I may be off, but the right columns are what in the example string those patterns match.

So depending on how closely your example string is to any actual strings you are using the pattern with, it may or may not be able to be reduced in size.

Only you can make that decision because you are the one that knows what strings you are working with and what you need to be done with them.

However, any pattern that finds the same matches as sub-patterns with an optional in it could probably be removed safely, no?

is it a technical term?

ok, but, I don’t understand why there are red characters.

Yes, I understand!

No, it is one of my idiosyncrasies to sometimes put neologisms (made up words) inside of quotes.

In this case, a simple regex pattern might be
the pipe character - the | - is a “logical OR”

So when I posted the “ors” I was referring to the patterns separated by the logical ORs
I was hoping you would understand that and I could get away cheap, but …

In this case they are meaningless. I used code fencing to preserve tabs and Discourse highlights code to make it easier to spot strings, functions etc. Very helpful for actual code examples, pseudo-tables not so much.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.