• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex for Optionals Cases  RSS feed

 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ! I wouldlike to know a regex formation who match with my exemple, please :/
It's composed of 3 of more elements separate by spaces.
First, the letter a which always needs to be here (compulsory).
After, a b "optional" element, which can be b1 or b2, b3... When I say optional that means this element can just be not here.
Then, other optionals elements like b: for c it will be c1 or c2, c3... BUT the trick is that when you don't write an element, the match needs to stop just before it. That means if you omit one optional element all the other on his right will don't match too. So do you understand ? I really hope yes, thank you in advance !
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you have a small fixed amount of letters, you can't do this with a regex.

Even if you could, I wouldn't do this with just a regex.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i always try to use multiple, simple regexes whenever possible, rather than one monolithic one. They're easier to understand, debug, change, and write. Then use the language itself (perl, java, whatever) for the logic.
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Unless you have a small fixed amount of letters, you can't do this with a regex.

Even if you could, I wouldn't do this with just a regex.

It's only for have a basis, if you try a regex with 1 alternative for optional element up to c I have my answer 🙂
fred rosenberger wrote:i always try to use multiple, simple regexes whenever possible, rather than one monolithic one. They're easier to understand, debug, change, and write. Then use the language itself (perl, java, whatever) for the logic.

I don't no how to code any languages yet, sorry.. And I can't divided it, I know it will be a complex regex but I have to do with that 😕 It's possible to add a regex for the begin/end of a match and one between both if it's helping you.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Menoux Mathieu wrote:Hi ! I wouldlike to know a regex formation who match with my exemple, please :/
It's composed of 3 of more elements separate by spaces.
First, the letter a which always needs to be here (compulsory).
After, a b "optional" element, which can be b1 or b2, b3... When I say optional that means this element can just be not here.
Then, other optionals elements like b: for c it will be c1 or c2, c3... BUT the trick is that when you don't write an element, the match needs to stop just before it. That means if you omit one optional element all the other on his right will don't match too. So do you understand ? I really hope yes, thank you in advance !

If it were me, I'd use String.split() plus other code instead of a regex. Is using a regex a requirement?
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Menoux Mathieu wrote:Hi ! I wouldlike to know a regex formation who match with my exemple, please :/
It's composed of 3 of more elements separate by spaces.
First, the letter a which always needs to be here (compulsory).
After, a b "optional" element, which can be b1 or b2, b3... When I say optional that means this element can just be not here.
Then, other optionals elements like b: for c it will be c1 or c2, c3... BUT the trick is that when you don't write an element, the match needs to stop just before it. That means if you omit one optional element all the other on his right will don't match too. So do you understand ? I really hope yes, thank you in advance !

If it were me, I'd use String.split() plus other code instead of a regex. Is using a regex a requirement?

Maybe not, I've heard I can add some code in but it seems complicated while a regex is easy ^^
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . .
But you are using a regex. Just in a different place. I presume that regex means any number of spaces greater than none.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Menoux Mathieu wrote:Hi ! I wouldlike to know a regex formation who match with my exemple, please :/
It's composed of 3 of more elements separate by spaces.
First, the letter a which always needs to be here (compulsory).
After, a b "optional" element, which can be b1 or b2, b3... When I say optional that means this element can just be not here.
Then, other optionals elements like b: for c it will be c1 or c2, c3... BUT the trick is that when you don't write an element, the match needs to stop just before it. That means if you omit one optional element all the other on his right will don't match too. So do you understand ? I really hope yes, thank you in advance !

The problem description is a bit vague. Perhaps some use cases would help clarify. Here are some use cases, but see if I'm on the right track.

"a b c" // optional 'b' and 'c' are not in alpha-followed-by-number format
"a b1 c1" // correct, complete match
"a b1 b2 b3 c1 c2 c3" // correct, complete match
"a b1 b2" // has 3 matches but no optional 'c' -- is this a match or not?
"a b1 b2 b9 c1 c2" // matches 'a' through 'b2', ignores everything to right of 'b2' -- missing opt 'c' -- is this correct?
"a b1 b2 b3 c1 c2 c9" // matches 'a' through 'c2', ignores everything to right of 'c2'


Perhaps you could tweak this list and add some more use cases.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To solve this problem, I would just scan through the tokens one by one, and see if the next token is either a continuation of the current letter's sequence, or if it starts the next letter's sequence.
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Carey Brown wrote:. . .
But you are using a regex. Just in a different place. I presume that regex means any number of spaces greater than none.

I know your line use Regex, but it also use Java and I don't know how to add this in my XML file 😕
Carey Brown wrote:
Menoux Mathieu wrote:Hi ! I wouldlike to know a regex formation who match with my exemple, please :/
It's composed of 3 of more elements separate by spaces.
First, the letter a which always needs to be here (compulsory).
After, a b "optional" element, which can be b1 or b2, b3... When I say optional that means this element can just be not here.
Then, other optionals elements like b: for c it will be c1 or c2, c3... BUT the trick is that when you don't write an element, the match needs to stop just before it. That means if you omit one optional element all the other on his right will don't match too. So do you understand ? I really hope yes, thank you in advance !

The problem description is a bit vague. Perhaps some use cases would help clarify. Here are some use cases, but see if I'm on the right track.

"a b c" // optional 'b' and 'c' are not in alpha-followed-by-number format
"a b1 c1" // correct, complete match
"a b1 b2 b3 c1 c2 c3" // correct, complete match
"a b1 b2" // has 3 matches but no optional 'c' -- is this a match or not?
"a b1 b2 b9 c1 c2" // matches 'a' through 'b2', ignores everything to right of 'b2' -- missing opt 'c' -- is this correct?
"a b1 b2 b3 c1 c2 c9" // matches 'a' through 'c2', ignores everything to right of 'c2'


Perhaps you could tweak this list and add some more use cases.

Thanks, I understand now my mistakes in my explanation 😃 So "a b c" // optional 'b' and 'c' are not in alpha-followed-by-number format is right, the next too but after not: "a b1 b2 b3 c1 c2 c3" // correct, complete match don't work because it must be only one a, b or c. So you can choose b1, b2 ,b3... but not two or more. Next, c element can be ignored, b element too, but c must follow b and so on. In this way, c element and the following can't match without b element. A regex which can work is "a((b1|b2|b3)(c1|c2|c3)?)?" but I it will be very difficult to use it if I had a lot of element, and this ignored the spaces between letters. So I try to do the same as you, I hope it will clarifies a bit 🙂
"a b c" // optional 'b' and 'c' are not in alpha-followed-by-number format: match "a"
"a" // correct, complete match
"a b2" // correct, complete match
"a b1 c1" // correct, complete match
"a c1 b1" failure, c element must be right after a 'b' existing element: match "a"
"a b1 b2" // failure, must only have one element for each letter: match "a b1"

 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm assuming that "a b3 c2 d3 f1 e5" matches up to and including "d3", and "f1" and "e5" are discarded.

Menoux Mathieu wrote:I know your line use Regex, but it also use Java and I don't know how to add this in my XML file

What XML file? You haven't told us about this.

Regardless, you can not solve this problem without plain code.
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:So I'm assuming that "a b3 c2 d3 f1 e5" matches up to and including "d3", and "f1" and "e5" are discarded.

Exactly 😃
Stephan van Hulst wrote:What XML file? You haven't told us about this.

Regardless, you can not solve this problem without plain code.

Yeah but that doesn't change much, no ? If you know what I'm talking about, it's a .tmlanguge file for a new Sublime Text syntax.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!