• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write a regular expression which has two letters separated by comma and specific combination  RSS feed

 
mahadeb ray
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to write a regular expression which has two letters separated by comma and specific combination will not allowed other than everything will allow.



I have a string 700XXX(5b,c). In this string XXX can be any letters and (5b,c) will not allowed to match but combination of e.g

700XXX(5a-z,c) except 'b' is not allowed e.g 700XXX(5d,c),700XXX(5f,c) is allowed like that

700XXX(5b,a-z) except 'c' is not allowed e.g 700XXX(5b,a), 700XXX(5b,k) is allowed like that

Can anybody help me to write regular expression for this string?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Do you mean that is there is a b before the comma, you are now allowed b after the comma. That looks like a context‑sensitive grammar which can never be expressed with a regular expression.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean without the c independent of what is before the comma, that is probably quite easy to write with a regex. Please show us what you have tried.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell is obviously much more astute than I am because having read the OP at least 5 times I still don't have a clue what the requirement is! I really don't understand how the terms such as "(5d,c)" are meant to constrain the XXX values; I'm not even sure they are meant as a constraint!
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:Campbell is obviously much more astute than I am . . .
Or he was simply guessing
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote: . . . Do you mean that is there is a b before the comma, you are now allowed b after the comma. . . .
Surely that should have said “not allowed”?
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea how applicable This is, but it provides a (very) short example on validating the British postal code, using regular expression, which has a very unique and distinct format. HIH
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends whether the text permitted is like a postal code; you can live at Aberdeen and have a postal code AB1 1AB. If using AB before the numbers would preclude using AB afterwards, then a regex would probably be impossible to create.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My thoughts are "Why do you think a regex is the way to do it?" I don't know if it is or not, but any time someone says "How do I use tool X to do Y?", it makes me think that you may be trying to drive a screw in with a hammer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!