Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
matching spaces
herald10
I have the following code snippet
$dcritem=qq{
<item name="item_id">
<value>$somevalue(\s)*</value>
</item>};
$something =~ s/\Q$dcritem\E/somestring/g;
Now if the variable $something has $somevalue followed by some blank spaces, I would like to be able match it. Obviously the above code is wrong. Can any one suggest me the correct way of doing it?
Thanks a lot
-H
Find more posts tagged with
Comments
Migrateduser
Does the * go inside the paren (just before the closing paren)?
herald10
I changed it. But still its not working.
Migrateduser
I suspect that the failure to match is occuring earlier than that, perhaps on the newlines after </value> or somewhere. Maybe try replacing the newlines with \s* and use the /x modifier if you still want the newlines in your code for readability. Mind you, that's just a suspicion - I don't see anything obviously wrong with what you have.
jbonifaci
What exactly are you trying to do?
If you're trying to find an item named "item_id" that has a value of "$something", surrounded by any amount of whitespace, and replace its value with "somestring", you could do something like this:
my $dcr = <<DCR;
<item name="item_id">
<value>somevalue </value>
</item>
DCR
print qq|DCR Before:\n$dcr\n|;
&replaceItemValue(\$dcr, 'item_id', 'somevalue', 'something');
print qq|DCR After:\n$dcr\n|;
sub replaceItemValue
{
my ($contentRecordRef, $itemName, $replaceFind, $replaceValue) =
@_
;
$$contentRecordRef =~ s|(<item name=\"$itemName\">\s*<value>)\s*$replaceFind\s*(</value>\s*</item>)|$1$replaceValue$2|g;
}
Adam Stoller
The newlines in the pattern will probably cause problems unless, as stated, you use the 'x' modifer - and even then it may still cause problems.
Newlines in the string being tested will probably cause problems unless you also use the 's' modifier.
\Q is going to turn \s into \\s which therefore will no longer match against white-space.
Perhaps instead of qq(...) you wanted to really use qr(...) - Look through perldoc perlop for more info...
Putting it altogether I think you'd want:
$dcritem = qr(<item name="item_id">\s*<value>$somevalue\s*</value>\s*</item>\s*)s;
$something =~ s|$dcritem|somestring|g;
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
Migrateduser
I've also found that sometimes a newline character SEEMS as though it'd be "\n", but in reality is "\r\n" (or maybe the other way around) -- my point is: there might be some other control characters that you might be missing. Do you have a good text editor with which you can see this stuff?
Dave
Migrateduser
According to the docs, the /s modifier is only supposed to alter the behaviour of the . metacharacter. By default it will match any character but a newline. With the /s it will match a newline as well. Since he isn't using ., it shouldn't matter in this case, but it is a good observation. As a general rule, if the stuff you are intending to match has newlines, you may need the /s and/or /m modifiers.
Incidentally, "How does the /s regex modifier differ from the /m modifier?" would be a great technical interview question for someone who claimed to know perl.
Migrateduser
Incidentally, "How does the /s regex modifier differ from the /m modifier?" would be a great technical interview question for someone who claimed to know perl.
If you asked this question in an interview I would laugh at you. Seriously, who knows details like this without looking it up? OK, clearly
you
know it, but unless you were hiring someone to do extensive regex work, what do interview questions like this prove? Evidently I don't know perl...but I've been faking it pretty well for the last 8 years or so.
Dave Smith
Sr. Software Engineer
Nike, Inc.
(503) 671-4238
DavidH.Smith@nike.com
Adam Stoller
Hmm - yes, in this case, without the anchoring and without the use of a '.' regex character you could get away without the 's' modifer.
The difference between the 's' and 'm' modifiers is a bit obscure - and I don't think I've ever, personally, run into a case where I needed the 'm' modifier - but it is kind of nice to know that it's there ... just in case ...
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
Migrateduser
It's a good question for detecting when someone is trying to snow you.
"I'm not sure, but I know they both have something to do with multi-line matches and I know where to look them up"
would be a perfectly acceptable answer.
I've built a whole career on being better than the average bear at finding answers in documentation.
Migrateduser
I would think there would be a lot more straightforward questions you could ask someone to find their depth of perl knowledge than asking about some obscure regex modifiers that people typically don't use in everyday life. I can look stuff up in a book, too, but I bet you wouldn't let me do that in your interview.
Dave Smith
Sr. Software Engineer
Nike, Inc.
(503) 671-4238
DavidH.Smith@nike.com
Migrateduser
Of course, it also helps that the person interviewing you knows the language they're trying to quiz you on. I've gotten this before:
"How is 'sprintf' different from 'print'"?
Yikes, where do I start??
Even questions like:
"How does declaring a variable with 'my' differ from declaring a variable with 'local'?"
Would be okay -- but many questions (like the 2nd one above) test your general programming language knowledge and are not Perl-specific.
Dave
Migrateduser
When I interviewed for my current position, they had me take the brainbench perl assessment as part of the technical interview. I am pretty fond of the way BB does things.
There were a number of really esoteric questions on the exam. Three of them involved type globs, and I have never in real life found myself thinking "wow, I could use a typeglob here". However, the result shows both your objective score and your percentile ranking. So, even though I only scored something like 3.86 out of 5.00, my percentile ranking was 96%.
When I've given technical interviews, I tend to focus pretty heavily on data structures and on regexes, because of the sorts of tasks I find myself and others around me needing to do. I can imagine that other folks would focus on different areas, for instance, I've seen some people really focus on how familiar the candidate is with the more popular core and CPAN modules, presumably to make sure the candidate isn't the sort of person who insists on re-inventing the wheel.
saumil
Hi
if you are looking for pattern for removing whitespace or the 'next line char'.
as u can't use chomp for removing 'next line char' as not always 'n'.
you can use
s/\s//gm;
for removing any whitespace or 'next line char'.
Regards,
Saumil