0

Given a file that contains code, my program reads the file using stream reader, and places it into a string. so for this file:

class A{

public void foo()
{
    r="asdastest@asdf.com";
    s="*asasdddsaa@test.asdf.com";
    t="@asdf.com";
    u="@pas.asdf.com nwe asda";
}

}

The string is:

"class A{\r\n \r\n public void foo()\r\n {\r\n  r=\"asdastest@asdf.com\";\r\n  s=\"*asasdddsaa@test.asdf.com\";\r\n  t=\"@asdf.com\";\r\n  u=\"@pas.asdf.com nwe asda\";\r\n }\r\n}\r\n\r\n\r\n"

(ignore \t which are replaced by " ").

I get a regular expression as an input,the regular expression uses the "$" to find a statement "before CRLF". The input regex is: (?i)\u0040.{0,10}asdf\u002Ecom($|\s|\W).* However when I execute the regex on the text- it finds only one result. If I add "enter" instead of one of the \r\n in the string that represents the code, the additional result will show. Since I cannot modify the regex, how can I read the file and get the 4 results i would expect?

The code that reads the file is:

 using (StringReader sr = new StringReader(ParsingHelpers.ReadFromFile(FilesExtensions.Instance, Configuration.Implementation.Configuration.Instance, LogEngineWriter.GetInstance(), curFile, "FindByRegex")))
                                {

                                    string bla=sr.ReadToEnd();
                                    bla=bla.Replace("\t", " ");
                                    fileStrBuilder.Append(bla);                                                                                      
                                }

The regex execution is a regular Match of the regex I wrote with Multiline and single line. The match result is:

@asdf.com\";\r\n s=\"*asasdddsaa@test.asdf.com\";\r\n t=\"@asdf.com\";\r\n u=\"@pas.asdf.com nwe asda\";\r\n }\r\n}\r\n\r\n\r\n"
mary
  • 789
  • 4
  • 10
  • 24
  • Add `\r?` before `$`. What is the regex flavor? – Wiktor Stribiżew Nov 07 '18 at 20:39
  • Are you using the multiline modifier? If it is in Java, I guess all you need is use `Pattern.UNIX_LINES`. – Wiktor Stribiżew Nov 07 '18 at 20:47
  • The regex is in .NET. I do use Multiline modifier. I tested the string and the regex on http://regexstorm.net/tester and it is equivalent to what I get in my program. Unfortunately I can't modify the regex. – mary Nov 07 '18 at 21:08
  • Can you include the match results? Also, can you read the file line by line and match that way? I'm guessing your 1 "match" is from the first @ to the end of the file? – Tezra Nov 07 '18 at 21:10
  • If you can't modify the regex, you cannot do anything. – Wiktor Stribiżew Nov 07 '18 at 21:10
  • since you can't modify the regex, you should include your programming language/code, as that is what you want to change, not the regex. – Tezra Nov 07 '18 at 21:13
  • i added the match above. – mary Nov 07 '18 at 21:19
  • @WiktorStribiżew I don't think this is a duplicate. Mary's problem is that the `.*` at the end of the regex is eating everything after the first match. – Tezra Nov 07 '18 at 21:34
  • @Tezra Then it is still a dupe, but of a different question. Looks like Mary is using `RegexOptions.Singleline`, so,it should be removed if `.` should not match across line breaks. – Wiktor Stribiżew Nov 07 '18 at 21:36
  • @WiktorStribiżew Can you switch the close to a more relevant question? – Tezra Nov 07 '18 at 21:40
  • @mary You should also include the code where you run the regex, but as Wiktor said, it sounds like you need to remove the single line regex option. – Tezra Nov 07 '18 at 21:45
  • If I remove the single line , I get the same result. – mary Nov 07 '18 at 22:19
  • If the .* isn't stopping on \n when you remove the single-line option, that implies that the new lines are being converted to the string literal "\n". I can't find the docs for ParsingHelpers.ReadFromFile, but try adding `bla=bla.Replace("\\n", "\n");` as well. – Tezra Nov 08 '18 at 13:40
  • It might be something with the Multiline after all. It seems like there is a bug somewhere further along that causes the regex to fail behaving as I expected. – mary Nov 08 '18 at 14:19

0 Answers0