Re: a small regexp question
by davis (Vicar) on Jul 29, 2005 at 09:21 UTC
|
Well, your example doesn't "take all characters except underscores". It substitutes underscores for spaces. If that's what you want, then
$string =~ s/_/ /g;
should work.
davis
Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
| [reply] [d/l] |
Re: a small regexp question
by Taulmarill (Deacon) on Jul 29, 2005 at 09:21 UTC
|
does it _have_ to be $1? you can get that string very simple by $string =~ tr/_/ /;. that will replace all underscores with spaces. if you want to keep the original string, just copy it into another scalar. | [reply] |
Re: a small regexp question
by bart (Canon) on Jul 29, 2005 at 10:20 UTC
|
You can get a list of all matches.
my @all = $string =~ /[^_]+/g;
From that, you can reconstruct a string from it, in whatever way you like. Like, using the default value for $", returns what you seem to want:
my $result = "@all";
| [reply] [d/l] [select] |
|
|
This is somewhat closer to what the OP seems to want, since it actually uses a match operator (although not $1 - I wonder if he will consider this to be acceptable!), but then you're using it to match everything that is not an underscore and collect it, in which case as you surely already know split would act the other way round -i.e. more directly- and would be the best tool:
my @all = split /_+/, $string;
| [reply] [d/l] [select] |
Re: a small regexp question
by Forsaken (Friar) on Jul 29, 2005 at 09:22 UTC
|
actually, from the looks of it, what you *really* want is a regex that simply replaces all the underscores by spaces. Assuming for a second you want the original string to remain intact how about this:
my $corrected_string = $string;
$corrected_string =~ s/_/ /g;
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: a small regexp question
by GrandFather (Saint) on Jul 29, 2005 at 09:22 UTC
|
I presume that you mean you wish to replace the underscores with spaces.
Here are a couple of variations that do that:
$string =~ s/_/ /g; # replace each _ with a space
$string =~ s/_+/ /g; # replace each run of _ with a single space
Perl is Huffman encoded by design.
| [reply] [d/l] |
Re: a small regexp question
by anonymized user 468275 (Curate) on Jul 29, 2005 at 09:41 UTC
|
$1 contains the part of the input string that actually matched the part of the regular expression enclosed in the first set of round brackets. Because "a sample string" is not a substring of "a_sample_string", then the following is true:
There does not exist a regular expression for which under the given circumstances $1 can possibly be "a sample string", sorry.
| [reply] |
|
|
Well, seeing the sort of things some people can do, I guess that there may be some regexen wizardry to achieve what the OP is after. I'm not really sure, though. And even if possible, I wonder why one should make his like hard like that, except, possibly, for a challange.
| [reply] |
|
|
Sure - you could for example write your own regexp engine to have different behaviour and patch it in, but I see the set of possible such wizardry as not intersecting with the set of acceptable solutions.
| [reply] |
Re: a small regexp question
by inman (Curate) on Jul 29, 2005 at 10:22 UTC
|
Use a regex to match a character class and assemble the result using a join. This has the advantage of normalising multiple underscores (or whatever was excluded by the character class) while preserving the original string.
my $found = join ' ', $string =~ /([^_]+)/g;
print "$found\n";
| [reply] [d/l] |
|
|
I'll say why the need this weirdness.There is a web application internally we used in our company for some purposes. Then we give a pattern in an entry box.An other entry box which accepts a regular expression. .It
uses matched value(not matched values!). And in that stuation,
I can't perform any substituon or transliteration ...only I have to put something into $1. I hope that clarifies something. N
| [reply] |
|
|
my $string = "a_sample_string";
$string =~ m/(?{ tr|_| | })(.*)/;
print $1;
That said, this is terribly fragile and dangerous. I wouldn't actually do that. You are essentially reaching out from the inside regex and altering the match variable before trying to match. It does manage to meet your criterion, though.
Update: Remember that this is actually a destructive match, though... you aren't just returning $1, but also altering the match input, wherever that comes from. This will fail if the match input is immutable.
| [reply] [d/l] [select] |
|
|
Re: a small regexp question
by anonymized user 468275 (Curate) on Jul 29, 2005 at 12:48 UTC
|
This appeared to break the exact stipulations of the OP, but on the other hand... at least the input and output rules are obeyed by this: $string="a_sample_string";
#...
my $disposable = $string; # put it in a disposable variable
$disposable =~ s/_/ /g; # substitution on that instead
$disposable =~ /^(.*)$/; # then put it in $1
| [reply] [d/l] |