Mastering Regular Expressions
Listings from pages 213 through 220
Download all listings shown below.
Chapter 5; page 213 (download)
Ten Thousand,10000, 2710 ,,"10,000","It's ""10 Grand"", baby",10K |
Chapter 5; page 213 (download)
# Either some non-quote/non-comma text . . . [^",]+ # . . . or . . . | # . . . a double-quoted field (inside, paired double quotes are allowed) " # field's opening quote (?: [^"] | "" )* " # field's closing quote |
Chapter 5; page 214 (download)
# Either some non-quote/non-comma text . . . ( [^",]+ ) # . . . or . . . | # . .. a double-quoted field (inside, paired double quotes are allowed) " # field's opening quote ( (?: [^"] | "" )* ) " # field's closing quote |
Chapter 5; page 214 (download)
while ($line =~ m{
# Either some non-quote/non-comma text . . .
( [^",]+ )
# . . . or . . .
|
# . . . a double-quoted field ("" allowed inside)
" # field's opening quote
( (?: [^"] | "" )* )
" # field's closing quote
}gx)
{
if (defined $1) {
$field = $1;
} else {
$field = $2;
$field =~ s/""/"/g;
}
print "[$field]"; # print the field, for debugging
Can work with $field now . . .
} |
Chapter 5; page 214 (download)
[Ten Thousand][10000][ 2710 ][10,000][It's "10 Grand", baby][10K] |
Chapter 5; page 215 (download)
(?:^|,)
(?:
# Either some non-quote/non-comma text....
( [^",]* )
# ... or...
|
# ... a double-quoted field (inside, paired double quotes are allowed)
" # field's opening quote
( (?: [^"] | "" )* )
" # field's closing quote
) |
Chapter 5; page 216 (download)
(?:^|,)
(?: # Now, match either a double-quoted field (inside, paired double quotes are allowed) . . .
" # (double-quoted field's opening quote)
( (?: [^"] | "" )* )
" # (double-quoted field's closing quote)
|
# . . . or, some non-quote/non-comma text . . .
( [^",]* )
) |
Chapter 5; page 217 (download)
import java.util.regex.*;
Pattern fieldRegex = Pattern.compile(
"\\G(?:^|,) \n"+
"(?: \n"+
" # Either a double-quoted field ... \n"+
" \" # field's opening quote \n"+
" ( (?: [^\"]++ | \"\" )*+ ) \n"+
" \" # field's closing quote \n"+
" # ... or ... \n"+
" | \n"+
" # ... some non-quote/non-comma text ... \n"+
" ( [^\",]* ) \n"+
" ) \n", Pattern.COMMENTS);
Pattern quotesRegex = Pattern.compile("\"\"");
// Given the string in 'line', find all the fields . . .
Matcher m = fieldRegex.matcher(line);
while (m.find())
{
String field;
if (m.group(1) != null) {
field = quotesRegex.matcher(m.group(1)).replaceAll("\"");
} else {
field = m.group(2);
}
// We can now work with the field . . .
System.out.println("[" + field + "]");
} |
Chapter 5; page 218 (download)
Imports System.Text.RegularExpressions
Dim FieldRegex as Regex = New Regex( _
"(?:^|,) " & _
"(?: " & _
" (?# Either a doublequoted field ...) " & _
" "" (?# field's opening quote ) " & _
" ( (?> [^""]+ | """" )* ) " & _
" "" (?# field's closing quote ) " & _
" (?# ... or ...) " & _
" | " & _
" (?# ... some non-quote/non-comma text ...) " & _
" ( [^"",]* ) " & _
" )", RegexOptions.IgnorePatternWhitespace)
Dim QuotesRegex as Regex = New Regex("""""") 'A string with two double quotes
Dim FieldMatch as Match = FieldRegex.Match(Line)
While FieldMatch.Success
Dim Field as String
If FieldMatch.Groups(1).Success
Field = QuotesRegex.Replace(FieldMatch.Groups(1).Value, """")
Else
Field = FieldMatch.Groups(2).Value
End If
Console.WriteLine("[" & Field & "]")
' Can now work with 'Field'....
FieldMatch = FieldMatch.NextMatch
End While |
Fetch additional Second-Edition listings and data: