faf38

Hi,
I had a problem with splitting string like that:
// begin "<filename>"
// folderName "<folderName>"
<content>
//end "<filename>"
// begin "<filename>"
// folderName "<folderName>"
<content>
//end "<filename>"
// begin "<filename>"
// folderName "<folderName>"
<content>
//end "<filename>"


Note: By <filename>, etc I have marked groups names.
I would like use NextMatch() method to get one file and next information from groups like filename, folder name, and file content.
Thanks for all hints.
Regards,
Lukasz



Re: Regular Expressions Splitting string into groups

OmegaMan

Try this


( (^//)
( ://[^"]*")( <Target>[^"]*)( :.*$)( :\n)
|
( <Target>[^/]*)
)


You will need to use Multiline and IgnoreWhiteSpace and extract the data from the named capture group Target such as

string value = myMatch.Groups["Target"].Value;





Re: Regular Expressions Splitting string into groups

faf38

Hi,
Big thanks for your help. Franky my question wasn't clear. I would like to get three groups:
- <filename>
- <content>
- <foldername>
from string like I have showed in previous post.
Currently I have following regex:


Code Snippet

static readonly Regex splitRegex = new Regex(@"^// name ""( <name>[^""]+)""\n^// begin ""( <filename>[^""]+)"". $\n( <content>.*)^// end ""\k<filename>"". $", RegexOptions.Multiline | RegexOptions.Singleline);


I would like to get folder name in code attach below.


Code Snippet

const string FilenameGroups = "filename";
const string ContentGroups = "content";
const string FolderNameGroups = "folderName";

public override void Execute()
{
DTE vs = GetService<DTE>(true);
projectItems = new List<EnvDTE.ProjectItem>();
for(Match match = splitRegex.Match(content); match.Success; match = match.NextMatch())
{
string relativeFileName = match.Groups[FilenameGroups].Value;
string fileName = Path.GetFileName(relativeFileName);
string relativeTargetPath = Path.GetDirectoryName(relativeFileName);
... here I would like to get folder name like:
string relativeFileName = match.Groups[FolderNameGroups ].Value

}
}


Sorry for misunderstanding.
regards,
Lukasz



Re: Regular Expressions Splitting string into groups

OmegaMan

The actual term is you want is to have the data placed into named match capture groups.

How about a small snippet of data The last thing we want to do is work on a regex and you come back and say, "Oh ya there is data in the content which looks like XXXX and your regex doesn't work.".





Re: Regular Expressions Splitting string into groups

faf38

Hi,
Sorry for that. To be precise the structure of a generated string is constant and shouldn't be different as pattern as I shown. I am generating a file after file and separate those file with:

//begin "here is file name and group name called filename"
here is any content I am not care about this
//end "here is file name and group name called filename"

however I would like to add to inside one file (between //begin and //end some pattern contains folder name). Regex which I have shown works good but I am unable to get folder name using string folderName = match.Groups["folderName"].Value;
Hope now I am more clear, if not please let me know i will give you more details.
Regards,
Lukasz





Re: Regular Expressions Splitting string into groups

OmegaMan

What I have done is created capture sub groups within the named match groups. You will need to enumerate over the groups["Key"] and groups["Value"] and extract the key value pairs. You will want to ignore the main match 0 and match 1 but concentrate on the named captured groups.


Input Text// begin "myfile.txt"
// folderName "C:\text"
MyContent1
//end "myfile2.txt"
// begin "myfile3.txt"
// folderName "C:\Windows"
MyContent2
//end "myfile4.txt"
// begin "myfile5.txt"
// folderName "D:\Working"
MyContent3
//end "Final.txt"
Regular Expression
(( ://\s )
( <Key>[^\s]*)
( :\s")
( <Value>[^"]*)
( :"[\r\n]{0,2})){1,3}
( <Content>[^/]*)
Group CapturesGroups: (0) (1) (Key) (Value) (Content)



Re: Regular Expressions Splitting string into groups

faf38

Hi,
Brilliant. This is what I need, frankly. Big thanks and once again sorry for my first, not clear post.
Regards,
Lukasz




Re: Regular Expressions Splitting string into groups

OmegaMan

After posting my final response I realized the overall pattern of begin to end. I thought the pattern was begin to content. If I could do it all over again, I would work it to do that.

Usage of groups within named capture groups moves you into a realm of regex processing that most people don't touch. Have fun!





Re: Regular Expressions Splitting string into groups

faf38

Hi,
Yes indeed, my pattern is of begin to end. If you could find a regex work with this pattern will be cool. I spend much time to make this work, especially that I am really beginner in regex processing Wink. However big thanks for all your help. Hope that you can help me to this create regex.
But also what is worth to mentioned is fact that I would like to get three values:
- filename
-foldername
- and a content
so e.g. input text can be:

// begin "myfile.txt"
// folderName "C:\text"
MyContent1
//end "myfile.txt"
// begin "myfile2.txt"
// folderName "C:\Windows"
MyContent2
//end "myfile2.txt"
// begin "myfile3.txt"
// folderName "D:\Working"
MyContent3
//end "myFile3.txt"


and I would like to get:
for match 0:
fileName equal myfile.txt
folderName equal C:\text
content equal MyContent1

TIA.
Regards,
Lukasz




Re: Regular Expressions Splitting string into groups

OmegaMan

Here is the regex, it no longer uses the named groups captures, but place all information into the named groups for begin to End.
Input Text// begin "myfile.txt"
// folderName "C:\text"
MyContent1
//end "myfile2.txt"
// begin "myfile3.txt"
// folderName "C:\Windows"
MyContent2
//end "myfile4.txt"
// begin "myfile5.txt"
// folderName "D:\Working"
MyContent3
//end "Final.txt"
Regular Expression
( ://\s begin\s")( <Begin>[^"]*)( :"[\r\n]{0,2})
( ://\s folderName\s")( <Folder>[^"]*)( :"[\r\n]{0,2})
( <Content>[^/]*)
( ://\s end\s")( <End>[^"]*)( :"[\r\n]{0,2})
Group CapturesGroups: (0) (Begin) (Folder) (Content) (End)



Re: Regular Expressions Splitting string into groups

faf38

Hi,
It is amazing what you can do with regex. Big thanks for all your effort to help mi with this stuff.
Regards,
Lukasz




Re: Regular Expressions Splitting string into groups

OmegaMan

If you begin to use regex in your everyday programming, you begin to realize its power and ease of use. Most never get over the learning curve and don't realize what they are missing.