Java regex matcher fails to work

295 Views Asked by At

Hello i have regex problem, my code is suposed to check a lot xml files and changes some places words. all is going ok till i use matches.replaceAll

i have looked into google and cant find solution. So code read each file line by line and check if in it something matches from pattern .

String patternStringDepOne = "(\\=(Dep)|\\=(Dep_LS)|(Dep)|(Dep_LS))((\\.dbo\\.)|\\.\\[dbo\\])";

There are multiple patterns, and each must be checked.

so code for checking

while(matcherDep.find()) { 
    if (matcherDep.group(2) != null) {
         if (matcherDep.group(2).matches("Dep") || matcherDep.group(2).matches("Dep_LS")) {
              String replaceAllDep = matcherDep.replaceAll(" <DEP>$6");
         }
    }
}

so this par check for total of 4 groups 2,3,4,5 and change match with word. My problem is that after a lot matches it gives me error, and i have no idea why, why its working before and after it doesn't work. if i comment out line with replace all it works. Error goes from one line to next depending how much replacing it does.

Error is:

java.lang.IllegalStateException: No match found
    at java.util.regex.Matcher.group(Matcher.java:536)

Also since in one while statement there are one if statement like in code for each group. and it usually fails at last if statement. But if i Write System.out.println just after While statement before if statements it prints out for one group founded match for rest null;

So any idea why its happening and how to fix it would be nice.

So in other words, i have about 680 xml files, in them in need to search for

Dep , [Dep] , Dep_LS and [Dep_LS]

And two other variables in similar configuration in all kinds possitions, Dep can be first word can be in middle of text, can be one can be multiple of them. I need to change them to for all four Dep variants. One xml file looks like this

Dep.dbo.ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd Dep.dbo.jodnsf.sdfj afsdasf adfa.sda.asdf asfd () das INNER JOIN Dep.dbo.ISIN_CALCULATED ISIN_FACT ON (Dep.dbo.ISIN.ISIN_ID=[Dep].dbo.EVENT.EVENT_ISSUER) INNER Dep.[dbo].ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd Dep.[dbo].jodnsf.sdfj afsdasf adfa.sda.asdf asfd () das INNER JOIN [Dep].[dbo].ISIN_CALCULATED ISIN_FACT ON (Dep.[dbo].ISIN.ISIN_ID=[Dep_LS].[dbo].EVENT.EVENT_ISSUER) INNER [Dep_LS].dbo.ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd Dep_LS.dbo.jodnsf.sdfj afsdasf adfa.sda.asdf asfd () das INNER JOIN Dep_LS.dbo.ISIN_CALCULATED ISIN_FACT ON (Dep_LS.dbo.ISIN.ISIN_ID=Dep_LS.dbo.EVENT.EVENT_ISSUER) INNER Dep.[dbo].ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd Dep_LS.[dbo].jodnsf.sdfj afsdasf adfa.sda.asdf asfd () das INNER JOIN Dep_LS.[dbo].ISIN_CALCULATED ISIN_FACT ON (Dep_LS.[dbo].ISIN.ISIN_ID=Dep_LS.[dbo].EVENT.EVENT_ISSUER)

finished result should be

<DEP>.dbo.ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd
<DEP>.dbo.jodnsf.sdfj afsdasf adfa.sda.asdf asfd () das INNER JOIN
<DEP>.dbo.ISIN_CALCULATED ISIN_FACT ON
(<DEP>.dbo.ISIN.ISIN_ID=[Dep].dbo.EVENT.EVENT_ISSUER) INNER
<DEP>.[dbo].ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd
<DEP>.[dbo].jodnsf.sdfj afsdasf adfa.sda.asdf asfd () das INNER JOIN
[Depend].[dbo].ISIN_CALCULATED ISIN_FACT ON
(<DEP>.[dbo].ISIN.ISIN_ID=[Dep_LS].[dbo].EVENT.EVENT_ISSUER) INNER
[Depend_LS].dbo.ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd
<DEP>.dbo.jodnsf.sdfj afsdasf adfa.sda.asdf asfd () das INNER JOIN
<DEP>.dbo.ISIN_CALCULATED ISIN_FACT ON
(<DEP>.dbo.ISIN.ISIN_ID=Dep_LS.dbo.EVENT.EVENT_ISSUER) INNER
<DEP>.[dbo].ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd
<DEP>.[dbo].jodnsf.sdfj afsdasf adfa.sda.asdf asfd () das INNER JOIN
<DEP>.[dbo].ISIN_CALCULATED ISIN_FACT ON
(<DEP>.[dbo].ISIN.ISIN_ID=Dep_LS.[dbo].EVENT.EVENT_ISSUER) Showing
14 matches Dep.dbo.ISINsdjndfadsfasd adsf.dsafadsf asd()dfasd

please bear in mind that this result is only from one paramaterString but there are 6 of them and each xml file should be checked by all 6 parameterStrings

Ok i got it working with regular expresion

(Depend(?:_LS)?|[Depend(?:_LS)?])(.dbo|.[dbo)

so now code inside while loop looks like

if (matcherDep.group(1) != null) { String replaceAll = matcherDep.replaceAll("<DEP>$2>"); }

0

There are 0 best solutions below