-3

Edited question because it was misunderstood.

Ex : I own these two strings of 5 pairs :

Line 1 : 0102030405

Line 2 : 0102203035

and I want to find 01 and 02 and 03 (for example) ; the expected result is :

matches : 0102030405

does not match : 0102203035

And if I want to find 01 and 02 and 30 (for example) ; the result I want is :

does not match : 0102030405

matches : 0102203035

Each pair is in a position modulo 2 (of course)

What regex pattern can I use ?

Thank you for the help !

fdav
  • 1
  • 3

3 Answers3

0

This simple and clear regex works fine for your case:

(010203)|(0102)|(01)

See Demo

54l3d
  • 3,782
  • 2
  • 24
  • 46
  • nice, but if i regex 01|02|30 i would like to get 2 second ligne but not the firs ... – fdav Nov 25 '15 at 19:10
0

Thank you to Laurent P., my next door neighbor. Laurent will recognize himself !

The ideal pattern is this: ^([0-9][0-9])*(02)([0-9][0-9])*(32)([0-9][0-9])*(39)([0-9][0-9])*(44)([0-9][0-9])*$ to find 02 and 32 and 39 and 44.

So, here is the mongo find using regex. db.collection.find({ g: { $regex: '^([0-9][0-9])*(02)([0-9][0-9])*(32)([0-9][0-9])*(39)([0-9][0-9])*(44)([0-9][0-9])*$' } })

fdav
  • 1
  • 3
  • Please do not answer (even partly) in languages other than English - it's confusing and unhelpful to most users of this site. Also, it's extremely unclear what this has to do with your original question. I'd suggest you edit both the question and answer to make them make more sense. – elixenide Dec 04 '15 at 04:53
-1

Assuming you're looking for all the pairs that start at an odd position:

^(?:(01|02|03)|\d\d)+$

Will capture all the numbers, but group only the pairs you need.

Ilya Kogan
  • 20,368
  • 15
  • 78
  • 134
  • It is exactly what i'm searching for but it fails on the online regex or with kiki ; i'll try with these search 01 02 and 30 witch normaly return's me only the second line – fdav Nov 25 '15 at 19:17
  • I think i need to pass 3 times each pattern : – fdav Nov 25 '15 at 21:27
  • I think i need to pipe each pattern : for exemple, i want only the line that contains 01 and 30 : first : find 01 on each odd position (^01)|(^..01)|(^....01)|(^......01)|(^........01) and then find on the result the 30 on each odd position (^30)|(^..30)|(^....30)|(^......30)|(^........30) this bring me the second line (string has only 5 pairs) – fdav Nov 25 '15 at 21:45
  • It's really hard to understand your question. Nobody has understood it yet. You need to be much more clear. – Ilya Kogan Nov 26 '15 at 05:50
  • You are true but you had understood my question : "looking for all the pairs that start at an odd position" on a string of 10 char. I did it with python but i'd like to know if a pattern exists with regex. I'll post the script ... – fdav Nov 26 '15 at 09:53
  • You said something about 30 which I don't understand because you didn't mention 30 in the question. Please edit the question to make it very very clear, otherwise I can't help you. – Ilya Kogan Nov 26 '15 at 16:35