15,904,500 members
Home / Discussions / Regular Expressions

# Regular Expressions

 Can anybody explain how this Regex works. fiaolle15-Nov-11 2:26 fiaolle 15-Nov-11 2:26
 Re: Can anybody explain how this Regex works. PIEBALDconsult15-Nov-11 5:04 PIEBALDconsult 15-Nov-11 5:04
 Re: Can anybody explain how this Regex works. fiaolle15-Nov-11 6:08 fiaolle 15-Nov-11 6:08
 Re: Can anybody explain how this Regex works. PIEBALDconsult15-Nov-11 6:34 PIEBALDconsult 15-Nov-11 6:34
 Re: Can anybody explain how this Regex works. fiaolle15-Nov-11 6:45 fiaolle 15-Nov-11 6:45
 Re: Can anybody explain how this Regex works. PIEBALDconsult15-Nov-11 13:46 PIEBALDconsult 15-Nov-11 13:46
 Re: Can anybody explain how this Regex works. Andreas Gieriet8-Apr-12 1:02 Andreas Gieriet 8-Apr-12 1:02
 Re: Can anybody explain how this Regex works. Andreas Gieriet7-Apr-12 12:06 Andreas Gieriet 7-Apr-12 12:06
See The 30 Minute Regex Tutorial and search for all occurances of `(?<=` in that article. This explains the meaning of `(?<=...)`.

You have always to separate the way you enter a pattern in C# and the pattern the Regex sees:

 C# @"..." pattern: `@"(?<=^(?:[^""]*""[^""]*"")*[^""]*) "` effective Regex pattern (here delimited by /.../): `/(?<=^(?:[^"]*"[^"]*")*[^"]*) /`

I'm now only talking in Regex domain (the 2nd row), not how it is entered in the C# string.

Let's start with the inner most part and work outwards:

1. `..."[^"]*"...`: "..."
2. `...[^"]*"[^"]*"...`: any number of non-"-char, followed by "..." from 1. above
3. `...(?:[^"]*"[^"]*")*...`: any repetition of the group described in 2. above
4. `...^(?:...)*...`: 3. above must match from the beginning of the text
5. `...^(?:...)*[^"]*...`: 4. above, followed by any number of non-"-char
6. `(?<=...) `: match a space that is preceeded by the expression from 5. above; the (?<=...) is not part of the match

The Regex searches for the space character and checks if the data before that space matches the prefix expression. If yes, the match is successful, otherwise, the Regex searches for the next space and checks again, etc.

The given Regex and the given data match only on one space, the one after `all`. The underlined part matches with `all`: `(?<=^(?:[^"]*"[^"]*")*[^"]*) `.

I.e. the regex splits the given data by spaces, respecting spaces within "..." strings as non-separators.

Very complicated, though. I would do this differently, namely in positive terms (what you want to be part of the fields rather than what splits them):

C#
```string pattern = @"\s*(""[^""]*""|\S+)\s*"; // maybe a more sophisticated pattern is
// needed since the above expression seems
// to match more,
// but this is maybe an undesired side
// effect of the complicated expression
string[] split = Regex.Matches(input, pattern).Cast<Match>().Select(m=>m.Groups[1].Value).ToArray();```

Cheers
Andi

modified 8-Apr-12 7:03am.

 Url rewriting rule Gaurav Goel (Team Lead)13-Sep-11 0:18 Gaurav Goel (Team Lead) 13-Sep-11 0:18
 Re: Url rewriting rule Shahriar Iqbal Chowdhury/Galib24-Sep-11 11:00 Shahriar Iqbal Chowdhury/Galib 24-Sep-11 11:00
 Re: Url rewriting rule AspDotNetDev24-Sep-11 11:37 AspDotNetDev 24-Sep-11 11:37
 Regular expression to check special character sri_34648-Sep-11 11:16 sri_3464 8-Sep-11 11:16
 Re: Regular expression to check special character Mizard X25-Sep-11 0:06 Mizard X 25-Sep-11 0:06
 Help with pattern Phrone7-Sep-11 20:25 Phrone 7-Sep-11 20:25
 Re: Help with pattern thatraja28-Nov-11 19:10 thatraja 28-Nov-11 19:10
 What is the Regular expression that detects a \$ sign and digits in html code Member 82168286-Sep-11 7:49 Member 8216828 6-Sep-11 7:49
 Re: What is the Regular expression that detects a \$ sign and digits in html code [modified] Paladin20006-Sep-11 10:54 Paladin2000 6-Sep-11 10:54
 Re: What is the Regular expression that detects a \$ sign and digits in html code Member 82168287-Sep-11 5:20 Member 8216828 7-Sep-11 5:20
 Re: What is the Regular expression that detects a \$ sign and digits in html code Paladin20007-Sep-11 5:27 Paladin2000 7-Sep-11 5:27
 Re: What is the Regular expression that detects a \$ sign and digits in html code Member 82168287-Sep-11 8:48 Member 8216828 7-Sep-11 8:48
 Re: What is the Regular expression that detects a \$ sign and digits in html code Pete O'Hanlon7-Sep-11 5:38 Pete O'Hanlon 7-Sep-11 5:38
 Re: What is the Regular expression that detects a \$ sign and digits in html code jschell7-Sep-11 8:32 jschell 7-Sep-11 8:32
 Regular Expression Morgs Morgan11-Jul-11 22:07 Morgs Morgan 11-Jul-11 22:07
 Re: Regular Expression Firo Atrum Ventus11-Jul-11 22:31 Firo Atrum Ventus 11-Jul-11 22:31
 Re: Regular Expression Morgs Morgan11-Jul-11 23:09 Morgs Morgan 11-Jul-11 23:09
 Last Visit: 31-Dec-99 18:00     Last Update: 23-May-24 21:43 Refresh ᐊ Prev1...242526272829303132 Next ᐅ

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.