dda has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks!
I need to add a (missing) single space after each punctuation sign in a string, but only if it is not a dot or comma inside a number. For example, "1,2,a,b" should be changed to "1,2, a, b". Also if there is already a space after punctuation sign, it should not be duplicated.
Right now I have this simple one for comma replacement:
$s =~ s/,(\S)/, $1/g;
But it spoils my numbers. Can you help? Is there a way to do it with one regular expression?
Re: Regex to add space after punctuation sign
by Abigail-II (Bishop) on Jan 08, 2004 at 09:20 UTC
|
Previous solutions only considered commas and periods, not
other punctuation symbols. Assuming anything that's not a
word character or whitespace is to be considered punctuation,
I would use:
s/(\d[,.]\d)|([^\w\s](?!\s))/$1 || "$2 "/ge;
Abigail | [reply] [d/l] |
|
Thank you, Abigail. Can you please explain how does the replacement part of your expression work? If I understand correctly, /e evaluates $1 || "$2 " -- that way if $1 is not empty, it should not add second part?
P.S. I added end-of-line processings as was suggested by aragorn:
s/(\d[,.]\d)|([^\w\s](?!\s)(?!$))/$1 || "$2 "/ge;
| [reply] [d/l] |
|
s/(\d[,.]\d)|([^\w\s])(?=\S)/$1 || "$2 "/ge;
A single positive look-ahead is, IMO, more clear than two
negative look-aheads.
Abigail | [reply] [d/l] [select] |
|
Re: Regex to add space after punctuation sign
by ysth (Canon) on Jan 08, 2004 at 09:19 UTC
|
$s =~ s/(?<=(?<!\d(?=[.,]\d))\p{P})(?!\s)/ /g;
Taking that apart, we are looking for a spot between two characters where a space should be inserted. We have a positive requirement for what comes before the spot (that's the (?<=...)) and a negative requirement for what comes after the spot. The after condition is simple. If there is a whitespace character (I'm guessing that you mean to include tab, newline, etc., based on your use of \S) this spot can be skipped.
The before condition is essentially (?<=\pP) with an additional condition. \pP matches any punctuation character. These are:
$ perl -wle'use charnames (); for (0..255) { chr($_) =~ /\pP/ and prin
+t chr($_)
," ($_): ", charnames::viacode($_) }'
! (33): EXCLAMATION MARK
" (34): QUOTATION MARK
# (35): NUMBER SIGN
% (37): PERCENT SIGN
& (38): AMPERSAND
' (39): APOSTROPHE
( (40): LEFT PARENTHESIS
) (41): RIGHT PARENTHESIS
* (42): ASTERISK
, (44): COMMA
- (45): HYPHEN-MINUS
. (46): FULL STOP
/ (47): SOLIDUS
: (58): COLON
; (59): SEMICOLON
? (63): QUESTION MARK
@ (64): COMMERCIAL AT
[ (91): LEFT SQUARE BRACKET
\ (92): REVERSE SOLIDUS
] (93): RIGHT SQUARE BRACKET
_ (95): LOW LINE
{ (123): LEFT CURLY BRACKET
} (125): RIGHT CURLY BRACKET
¡ (161): INVERTED EXCLAMATION MARK
« (171): LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
· (183): MIDDLE DOT
» (187): RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
¿ (191): INVERTED QUESTION MARK
Plus several more unicode characters if you have utf8 data.
If you mean a more restricted set of characters, use a character class like [.!,;] or whatever.
The additional condition is (?<!\d(?=[.,]\d)): this stipulates that what comes before the punctuation should not match \d(?=[.,]\d); that is a single digit, where the digit is followed by a period or comma and another digit. Note that that lookahead is actually escaping the bounds of the outer lookbehind, but that is perfectly ok to do.
Update: added actual character to punctuation table
Update: weird. backtick (GRAVE ACCENT) isn't considered punctuation | [reply] [d/l] [select] |
Re: Regex to add space after punctuation sign
by grinder (Bishop) on Jan 08, 2004 at 08:42 UTC
|
#! /usr/bin/perl
use strict;
while( <DATA> ) {
s/(?<!\d), *|, *(?=\D)/, /g;
print;
}
__DATA__
1,2,a,b
1,a,2,b
1,a,b,2
a,1,2,b
__produces__
1,2, a, b
1, a, 2, b
1, a, b, 2
a, 1,2, b
But the use of alternation strikes me a bit as cheating. There is no doubt ABWTDI, but I can't see it for the moment. | [reply] [d/l] |
Re: Regex to add space after punctuation sign
by Aragorn (Curate) on Jan 08, 2004 at 09:23 UTC
|
I found a solution without using look-behind, which has the limitation of not being able to match variable length strings (so /(?<!\t*)foo)/ does not work).
#!/usr/local/bin/perl
use strict;
while (my $str = <DATA>) {
chomp($str);
$str =~ s/(\d+[.,]\d+ # Substitute a (simple) number ...
| # or ...
[^.,\d ]+) # something without periods, comma's,
# digits or spaces ...
((?!$)[.,]?) # followed by a period or comma, if
# possible, but not when at the end of
# the line ...
/$1$2 /gx; # by the number or string, followed by
# the found period or comma, and a
# trailing space
print "<$str>\n";
}
__DATA__
1,2,a,b
a,1,b,2,
1,21,a,b,word,d.3,4.5.6
__END__
<1,2, a, b>
<a, 1,b, 2,>
<1,21, a, b, word, d. 3,4. 5.6>
In addition to the on-line documentation, you could check out Mastering Regular Expressions, 2nd Edition.
Arjen | [reply] [d/l] [select] |
Re: Regex to add space after punctuation sign
by BrowserUk (Patriarch) on Jan 08, 2004 at 09:39 UTC
|
I think most of the solutions so far will fail to put a space into 'x34,3b'.
There is also the question of what to do with '2,3,4,5'? Is that '2,3, 4,5' or '2, 3,4, 5' or '2, 3, 4, 5'? All are possible.
It will probably be very hard to use a single regex to do this if non-numeric bits can start and/or end with a digit.
I got close using s///e, but it was very messy.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
| [reply] |
Re: Regex to add space after punctuation sign
by davido (Cardinal) on Jan 08, 2004 at 08:42 UTC
|
This seems to pass my limited testing...
use strict;
use warnings;
my $string = '1,2,a,b,3';
$string =~ s/( (?:(?<!\d)(?:,|\.)(?!\s*\d)) |
(?:(?<=\d)(?:,|\.)(?=\s*)(?!\d)) |
(?:(?<!\d)(?:,|\.)(?=\s*\d)) )
/$1 /gx;
print $string, "\n";
It's really ugly, but triggers a substitution all of the following cases:
- Comma or dot is not surrounded by digits, with optional whitespace after the comma or dot.
- Comma or dot doesn't have a digit to the left, with optional whitespace after the comma or dot.
- Comma or dot doesn't have a digit to the right, with optional whitespace after the comma or dot.
So I think it meets your spec. If this is homework I would seriously recommend immersing yourself in the POD's so that you'll be able to explain to your professor how it is you came up with such a wild RE. For that matter, it's time that I re-read the POD's, as I'm not convinced that it really needs to be so explicit to work properly.
See perlre to learn about zero-width lookahead and lookbehind (positive and negative). Also read perlretut and perlop under the "Regexp quote like characters" section, for starters.
| [reply] [d/l] |
Re: Regex to add space after punctuation sign
by Roy Johnson (Monsignor) on Jan 08, 2004 at 17:50 UTC
|
No alternation, no /e, just soft and pink.
s/(?!\d[.,]\d)(.[^\w\s])(?!\s)/$1 /g;
Update
Just noticed how similar this is to ysth's solution, but his use of lookahead/lookbehind is really advanced.
...and...
Had removed the dot from the group. Need that. (Thanks, dda.)
The PerlMonk tr/// Advocate
| [reply] [d/l] |
|
$_ = "1.2.a.b";
s/(?!\d[.,]\d)([^\w\s])(?!\s)/$1 /g;
print;
Prints "1. 2. a. b"
| [reply] [d/l] [select] |
Re: Regex to add space after punctuation sign
by dda (Friar) on Jan 08, 2004 at 09:22 UTC
|
Thank you guys! It is not a homework. I'm developing a help desk system, and when users add comments and list some objects (cities actually) delimited with comma without spacing, it destroys page layout.
| [reply] |
Re: Regex to add space after punctuation sign
by Hena (Friar) on Jan 08, 2004 at 09:53 UTC
|
I'm not quite sure got this right but heres my try (both dot and comma is used).
s/(?<=\D)([,.]) *(?=\D)/$1 /g;
Tested with this.
perl -e '$a="1,23, 23, df,sd.asf,\"\"";$a=~s/(?<=\D)([,.]) *(?=\D)/$1
+/g;print "$a\n";'
1,23, 23, df, sd. asf, ""
| [reply] [d/l] [select] |
|
|