Boberts has asked for the wisdom of the Perl Monks concerning the following question:
Hey I'm looking for a regex (at least I think what is most likely a regex) to help clean up some text
The text has to consist of (i) only consist of ASCII letters, ASCII digits, apostrophes, hyphens and underscores; (ii) start with a letter or start with an apostrophe followed by a letter, (iii) do not contain a sequence of two or more apostrophes or hyphens, and (iv) end with (a) a letter, (b) a digit, or (c) an apostrophe preceded by the letter s, should be considered to be words.
Any help is appreciated
Re: Looking for a Regex
by AnomalousMonk (Archbishop) on Mar 03, 2019 at 21:08 UTC
|
Some other resources: perlre, perlretut, perlrecharclass, Regexp Quote-Like Operators, and Pattern Matching, Regular Expressions, and Parsing.
Also see Short, Self Contained, Correct (Compilable), Example and How to ask better questions using Test::More and sample data. For the latter, both for asking questions and for general development, maybe something like:
use warnings;
use strict;
use Test::More 'no_plan';
use Test::NoWarnings;
my @TESTS = (
# dirty # clean
[ q{The 99th bottle's x-o-x labels'} => q{The 99th bottle's labels'}
+ ],
...
);
VECTOR:
for my $ar_vector (@TESTS) {
if (not ref $ar_vector) {
note $ar_vector;
next VECTOR;
}
my ($dirty, $expected) = @$ar_vector;
my $clean = clean_it_up($dirty);
ok $clean eq $expected, qq{'$dirty' -> '$clean'};
} # end for VECTOR
done_testing;
exit;
# subroutine(s) ##############################
sub clean_it_up { ... }
...
See Test::More and friends.
Update: Changed to using q{...} in example code test sentences (was '...') because it's more compatible with embedded single-quotes. (Also a trivial wording change.)
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
Re: Looking for a Regex
by 1nickt (Canon) on Mar 03, 2019 at 15:59 UTC
|
Hi, $help ne $doing_it_for_you. What have you tried?
(Update: Also posted a link to perlrequick in CB in response to OP)
The way forward always starts with a minimal test.
| [reply] [d/l] |
Re: Looking for a Regex
by davies (Prior) on Mar 03, 2019 at 18:50 UTC
|
I, too, believe that what you want is a regex. I also agree with 1nickt that this isn't a code writing service. Assuming from your uncertainty that you are unfamiliar with regexes - a terrifyingly complex subject - let me try to get you started.
Character classes seem to be what you want. There are lots of resources on line, for example https://www.regular-expressions.info/charclass.html. You may or may not need different classes for each of your parts. A class for letters, digits and the other three you mention might be:
[a..z0..9'-_]
Using i after the final slash will make the regex case insensitive, meaning that you don't need to include upper case letters as well.
If you are familiar with testing, I recommend writing this as a series of tests to build up the regex one requirement at a time. Your test file will then explain to you in six months' time what you were trying to do. You won't remember otherwise. If you aren't, testing is a very useful tool to learn.
Regards,
John Davies | [reply] [d/l] [select] |
Re: Looking for a Regex
by BrowserUk (Patriarch) on Mar 03, 2019 at 20:50 UTC
|
if(
m[
^'?[a-z] ## start with letter or an apost
+rophy followed by one
[a-z0-9'-_]+ ## Alphanumeric + '-_
(?[a-z0-9] | s') $ ## ends with alphanumeric or
]x
and not
m[''|--]
}{
## Ok!
}
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
Suck that fhit
| [reply] [d/l] |
|
Nice! At first I thought what a shame it was that you would provide code-writing service for someone who showed no effort, but then I realised you were really providing code that does not compile, so as to force the OP to show effort. Sneaky!
The way forward always starts with a minimal test.
| [reply] |
|
... so as to force the OP to show effort.
And if you'd kept that to yourself, we might have found out if the OP had tried it.
With following the error messages and just a little effort, what I posted can become:
#! perl -slw
use strict;
for( qw[
ab123
'ab123s
'ab123's
] ) {
if(
m[
^'? [a-z] ## start with letter or an apostro
+phy followed by one
[a-z0-9'-_]+ ## Alphanumeric + '-_
(?: [a-z0-9] | s') $ ## ends with alphanumeric or s'
]x
and not
m[''|--] ## No doubled ''s or --s
){
print "$_: Ok!";
} else {
print "$_: Bad!";
}
}
__END__
C:\test>junk
ab123: Ok!
'ab123s: Ok!
'ab123's: Ok!
Which gives a starting point but not the whole thing.
If you're going to bother saying something, best make it something useful.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
Suck that fhit
| [reply] [d/l] |
|
|
|
Re: Looking for a Regex
by Laurent_R (Canon) on Mar 04, 2019 at 09:05 UTC
|
I'm looking for a regex (at least I think what is most likely a regex)
It could possibly be one regex, but it would more likely be several regexes.
Besides, you did not clearly specify whether you just want to validate that the text used satisfies your requirements, or whether you actually want to clean up the piece of text (i.e. remove characters that violate your requirements).
| [reply] |
|
|