'Regex ignore first x characters and then match pattern
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.
Solution 1:[1]
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 characters0000
: 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.
Solution 2:[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})
You can test it here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | trincot |
Solution 2 | Tahir Hussain Mir |