Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How to match negative number in a line.
input line :strongest_signal_indication Invalid {-1} some text
I was trying this regex expression:
/ ( [+-]?\d+ )| (\s+ ( \{ [^-]+ \} |\S+)) /xg
but it did not work for me
Any help please
Your pattern actually does match.
$ perl -E'
say
"Invalid {-1}" =~ / ( [+-]?\d+ )| (\s+ ( \{ [^-]+ \} |\S+)) /x
? "match" : "no match";'
match
In fact, / [+-]? \d+ /x would have sufficed.
Related
This question already has answers here:
How to use different separators (/ , |) in a regular expression
(2 answers)
Closed 1 year ago.
While trying to debug an issue I am having with git-svn and the --ignore-paths option, I've run into this perl expression that I don't understand and haven't been able to find anything similar in perl documentation.
$path =~ m!$self->{ignore_regex}!
My understanding of this is that this is matching the $path string to the ignore_regex but it doesn't seem to match anything the way I expect. The part that I don't understand is the m! ! around $self->{ignore_regex}?
How should I be reading this syntax?
This is the match operator.
m!...! is more commonly written as /.../, but they are 100% identical.
If "/" is the delimiter then the initial m is optional. With the m you can use any pair of non-whitespace (ASCII) characters as delimiters. This is particularly useful for matching path names that contain "/", to avoid LTS (leaning toothpick syndrome). [...]
For example, the following are identical:
$path =~ /$self->{ignore_regex}/
$path =~ m/$self->{ignore_regex}/
$path =~ m^$self->{ignore_regex}^
$path =~ m{$self->{ignore_regex}}
The code in question checks if the string in $path matches the regex pattern in $self->{ignore_regex}.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I revised my code and realized I stored the regex inside constant and then used the latter's value for the variable
I'm trying to store a regular expression inside variable constant using the qr// operator. Everything is fine except for '^' and '$'. I need them to match beginning-of-line and end-of-line respectively.
use constant REGEX_LINE => qr/\^(\s*)(.*)\$/;
my $rx = REGEX_LINE;
Printing $rx reveals that it contains some addiotional stuff:
(?^:^(\s*)(.*)$)
Of course now the regex doesn't match my data
If you expect ^ and $ to match start and end of line,
don't escape them (or else they will match ^ and $), and
use /m (or else they will match the start and end of the string).
use constant REGEX_LINE => qr/^(\s*)(.*)$/m;
Add escape character(\) before $ symbol otherwise it will consider as a part of variable
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to write the following statement as a Perl search expression:
Find all occurrences of the word "cat" followed by the word "dog" within 13 characters.
So for example the text "catajdwos dogqwzv" would be a result.
Do someone know how to do this?
I'd use a regular expression evaluated by the match operator. You'd use the g modifier of the match operator to find all occurrences.
while ($str =~ /...pattern.../g) {
...
}
Refer to your class notes on regular expressions to compose the pattern you need.
The following should work...
$str = "sdfcatsdfdogffdfcatsdfjljlfflkfjflkjfdogsfsd";
#arr = $str =~ /(cat).{0,10}dog/sgi;
print join(',', #arr), "\n";
s to match over newlines, g to extract all matched instances, and i to ignore case.
I'm not sure what you mean by 'within 13', but I've assumed here that as many as 10 characters can separate the 't' in cat from the 'd' in dog.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file containing lines as below.
#user
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode;
#user1
code1code1code1code1code1code1
code1code1code1code1code1code1
code1code1code1code1code1code1
code1code1code1code1code1code1;
#user2
code2code2code2code2code2code2
code2code2code2code2code2code2
code2code2code2code2code2code2
code2code2code2code2code2code2;
#user (again "user" but with a different code)
code3code3code3code3code3code3
code3code3code3code3code3code3
code3code3code3code3code3code3
code3code3code3code3code3code3;
I want extract only codes from the "user", the output I'm looking for is:
#user
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode;
#user
code3code3code3code3code3code3
code3code3code3code3code3code3
code3code3code3code3code3code3
code3code3code3code3code3code3;
Results retuned only the lines matching "user" and its respective codes.
I tried awk -F";" '{print $1}' $file but i cant isolate codes from a specific user.
In a perl one liner:
perl -ne 'print if /^#/' in.txt
You can use awk as follows:
awk -F\; '/code/' test.txt
Assuming the field delimiter is ; and the pattern is code.
If you want to print a particular line or column, use awk's NR and NF variables respectively.
For more info: http://www.gnu.org/software/gawk/manual/gawk.html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for a quick and efficient way to double quote all fields in tab delimited or comma separated text files.
Ideally, this would be a Perl one-liner that I can run from the command-line, but I'm open to any kind of solution.
Use Text::CSV:
perl -MText::CSV -e'
my $c = Text::CSV->new({always_quote => 1, binary => 1, eol => "\n"}) or die;
$c->print(\*STDOUT, $_) while $_ = $c->getline(\*ARGV)' <<'END'
foo,bar, baz qux,quux
apple,"orange",spam, eggs
END
Output:
"foo","bar"," baz qux","quux"
"apple","orange","spam"," eggs"
The always_quote option is the important one here.
If your file does not contain any double quoted strings containing the delimiter, you can use
perl -laF, -ne '$" = q(","); print qq("#F")'
awk -F, -v OFS='","' -v q='"' '{$0=q$0q;$1=$1}7' file
for example, comma sep:
kent $ echo "foo,bar,baz"|awk -F, -v OFS='","' -v q='"' '{$0=q$0q;$1=$1}7'
"foo","bar","baz"
tab sep would be similar.