0
String = '11111111111110000000000000000000110000000000000011111111111111111111111111111111110011111111111110000011110000011111111111110000000000011111111111111111010001111111111111111111110011111111111111111111111111110111112111121111111111111111111000011000001011111111111101022111101111001111111111110000001000000111111111111111000000000000011111111111111100011111111001011111111100000000000000000000000000000000100111001000000000000000000011000000000000001111111000000000000000000000000000000000001111100000000000000000000011000000000000000000000010000000000333333333'

I want a pattern to take out 10 characters after the first 100 so i want to have 100 - 110 then I want to compare that one and see if that string with a length of 10 have 4 zeros in a row.

How can I do this with only Regex? I have been using substring before.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Jonatan Lundqvist Medén
  • 2,750
  • 1
  • 17
  • 15

2 Answers2

5

You could use this:

^.{100}(?=.{0,6}0000)(.{10})

Explanation:

  • ^: matches the start of the string to avoid that the pattern is used anywhere in the input
  • .{100}: match 100 characters
  • (?= ): look ahead. This does not capture, but just verifies something that is still ahead.
  • .{0,6}: 0 to 6 characters
  • 0000: literally 4 zeroes
  • (.{10}): 10 characters, this time they are captured and can be referenced back with \1 or $1 depending on the flavour of regex.
trincot
  • 317,000
  • 35
  • 244
  • 286
2

The above answer is perfect. But that matches all the characters including first 100. In case of ignoring first 100, we can use

(?<=.{100})

To check the required pattern in last 10 characters after first 100 only, we can use

(?<=.{100})(?=.{0,6}0000)(.{10})
Thom A
  • 88,727
  • 11
  • 45
  • 75
Tahir Hussain Mir
  • 2,506
  • 2
  • 19
  • 26