0

I want to go through a list of header files, and save the files which those include. My problem is that the pattern does not match.

In this link you can find the pattern which I thought will work: https://regex101.com/r/jbJLxT/3

string rgxPat = "\\#include\\s+\"(?:\\w+\\/)*(\\w +\\.(?:hed|he|hdb|h))\"";
Regex incLRgx = new Regex(rgxPat, RegexOptions.IgnoreCase);
for (int i = alrdyChckd; i < missFiles.Count; i++)
{
    tmpStr = baseSBFolder + "\\" + missFiles[i].getPath() + "\\" + missFiles[i].getName();
    System.IO.StreamReader actFile = new System.IO.StreamReader(tmpStr);
    while((actLine = actFile.ReadLine()) != null)
    {
        Match match = incLRgx.Match(actLine);
        if(match.Success)
        {
    missFiles.Add(baseSB.getFileByName(match.Groups[1].Value.ToString()));
        }
    }
    alrdyChckd++;
}

I checked the debug varaibles and the match function always give back false return value, while the pattern and the actual line seems to be the same. Also it's a problem that I cannot add double qoutes as I wanted with the string = @"[pettern]" form because the double queste will close the pattern.

3
  • Are you trying to get the entire path or the basename only? Commented Jul 30, 2019 at 14:41
  • Look into using/IDisposable for your StreamReader and also Path.Combine() instead of all that string concatenation with hard-coded `\`. Commented Jul 30, 2019 at 14:55
  • The only issue is a typo: you inserted a space between \w and +. Replace \\/ with / in the C# code. match.Groups[1].Value.ToString() = match.Groups[1].Value Commented Jul 30, 2019 at 15:30

2 Answers 2

2

This will give you the paths:

/^\#include\s+"(.+)"$/gm

Output of $1:

FSW/CustSW/CustSW_generic/RSC/Src/gen/rsc_cpif.h
EbsPartition/EbsCluster/EbsCluster_generic/EbsCore/Src/ebscore_basetypes.h
FSW/CustSW/CustSW_generic/RSC/Src/gen/rsc_types.h

If you want just the filenames then use:

/^\#include\s+".*\/([^\/]+)"$/gm

and $1 will give you:

rsc_cpif.h
ebscore_basetypes.h
rsc_types.h
Sign up to request clarification or add additional context in comments.

Comments

0

You can capture the group by name to get the file path:

\#include(?<path>\s+"(?:\w+\/)*(\w+\.(?:hed|he|hdb|h))")

match.Groups["path"].Value.ToString()

This will give you the file path captured as "FSW/CustSW/CustSW_generic/RSC/Src/gen/rsc_cpif.h"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.