Im writing battleship valdiator on C# and want to use Regex to deal with it. Particulary, I
m trying to check, that some characters are RIGHT OVER OTHER ones. So i`m using codeblock, that should do like that(i hope you will decode me): Regex.Matches(fieldS,
“000000([0-9]+at least one instance ofn){strlength-“000000”.Length}011110([0-9]+at least one instance ofn){strlength-“011110”.Length}000000″)
namespace Solution {
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
public class BattleshipField {
public static bool ValidateBattlefield(int[,] field)
{
string fieldS = String.Join("",field.Cast<int>()
.SelectMany((x,index)=>index==0?
new String("000000000000n0"+x):
index==field.Length-1?
new String(x+"0n000000000000"):
index%10==9?
new String(x+"0"+"n"):
index%10==0?
new String("0"+x):
new String(x+"")).ToList());
if(Regex.Matches(fieldS,
"000000[0-9,n]{8}011110[0-9,n]{8}000000").Count()+
Regex.Matches(fieldS,
"000n010[0-9,n]{11}010[0-9,n]{11}"+
"010[0-9,n]{11}010[0-9,n]{11}000").Count()
!=1){
Console.WriteLine(4+"n"+fieldS);
return false;}// checking for battleship of length 4; 3,2,1 - analogically
return true;
}
}
}
Ive searched for alike problems, but didn
t find any answer. I want to get Regular Expression able to produce needed result.