0

I am writing a regular expression which has multiple permutations and combinations. I did write the regex individually but how to combine them to make it a single regex. Please guide.

Allowed strings:

  • combination of alphabets and numbers
  • can have space in between
  • can have special characters at start, end and in between also (cannot have # + \ ,)
  • allowed examples for reference dfg, rt534, 34dfg, dfg(space)564(space)dfg545, !sdf7&*

Not allowed strings

  • cannot have space at start and end
  • cannot have # + \ , anywhere in the string
  • cannot start with number but can be followed by an alphabet
  • Ex: allowed - 445fsdf
  • Ex: not allowed - 4
  • not-allowed examples for reference 345, (space)sdfs, sdfsdf(space), #df8, sdfsdf(comma)4534

The individual regex I have are

^[A-Za-z0-9]*$
[^#+\\\,]
^[^\s]+(\s+[^\s]+)*$

Please guide me in combining the regular expressions.

Anna
  • 1,669
  • 7
  • 38
  • 63
  • So do all space-seperated substrings have to follow the same rules? For example; is 123ab 123 allowed or not? Assuming they don't, have a go with [`^(?!\d+\b|.*[#+\\,])\S+(?: \S+)*$`](https://regex101.com/r/q1IqWE/1) – JvdV Aug 17 '23 at 07:32
  • The individual RegExs are fairly easy to understand and should be easy to combine with some conditional code. However, merging them into one big RegEx is likely to deliver something that is difficult to understand and difficult to maintain. As you need help in doing the merge I wonder how well you would understand the result and whether you will be able to maintain it. – AdrianHHH Aug 17 '23 at 07:34
  • What is "b/w", please do not use uncommon abbreviations without explaining them. – AdrianHHH Aug 17 '23 at 07:35
  • @JvdV they do not follow the same rules. – Anna Aug 17 '23 at 07:50
  • @AdrianHHH thats what I have to learn and hence asking for help. b/w means between, I have corrected my query. – Anna Aug 17 '23 at 07:52
  • @Anna, so what about the link provided in my comment? Would that work for your case? – JvdV Aug 17 '23 at 07:56

2 Answers2

2

If you want to replicate logical AND with regex, there is a bigger answer, which I won't attempt to replicate. Please read Regular Expressions: Is there an AND operator?

However, if you want to replicate logical OR with regex you could use alternation like this:

(patternA | patternB | patternC)

e.g.

(^[A-Za-z0-9]*$ | [^#+\\\,] | ^[^\s]+(\s+[^\s]+)*$)

A very concise summary of this operator (from the linked reference) is: "it tells the regex engine to match either everything to the left of the vertical bar, or everything to the right of the vertical bar."

M H
  • 325
  • 3
  • 8
  • That excludes the valid `-445fsdf` – mplungjan Aug 17 '23 at 07:33
  • I am not addressing the patterns provided, just how to logically operation OR on them. I think the greatest benefit of this question to the SO community is in the sharing awareness of Alternation, not just specific regex patterns. – M H Aug 17 '23 at 07:37
  • I think the requirement is to **AND** the three Regexes. **OR**ing them is not useful. – AdrianHHH Aug 17 '23 at 07:38
  • In that case, see this answer (and others like it): https://stackoverflow.com/questions/469913/regular-expressions-is-there-an-and-operator. I'll edit my answer to include this. – M H Aug 17 '23 at 07:43
1

You could use this regex, which combines the conditions that you state in the question:

^(?! )(?!.*\s$)(?=.*[a-zA-Z])[^#+\\,]+$

It matches a string which:

  • (?! ) : doesn't start with (space)
  • (?!.* $) : doesn't end with (space)
  • (?=.*[a-zA-Z]) : contains at least one alphabetic character
  • [^#+\\,]+ : consists only of characters not including #, +, \ or ,

Regex demo on regex101. Note for the purposes of the demo I had to include \n in the inverted character class to prevent matching text over line boundaries.

Nick
  • 138,499
  • 22
  • 57
  • 95