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 4 years ago.
Improve this question
I am trying to find a pattern to remove % from the text. This could have been easily achieved through ESCAPE or replace but I am restricted to only modify the contents of $pattern .
$text = "something is 5% and 10% value"
$pattern = "[^!%]*" // only this can be modified.
([Regex]::Match($text,$pattern)).value
Output should be :
something is 5 and 10 value
From the documentation:
Remarks
The Match(String, String) method returns the first substring that matches a regular expression pattern in an input string.
If you can only modify the pattern but not the rest of the code it's not possible to achieve what you want.
You need somthing like
$pattern = '[!%]'
[regex]::Replace($text, $pattern, '')
or
$pattern = '[!%]'
$text -replace $pattern
Related
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 1 year ago.
Improve this question
I need a Perl Regex to match expressions that not have any string from a set of strings
As a example, suppose the set of strings {"red", "blue", "white"}
"A blue sky" Not matching the rule
"A green house" Match the rule
The regex (red|blue|white) match the rule when a string is present, but I need the opposite
The !~ operator will return true if a string does not match a regular expression:
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;
my #phrases = ("A blue sky", "A green house");
for my $phrase (#phrases) {
say "'$phrase' matches the rule" if $phrase !~ /red|blue|white/;
}
Alternatively,
say "'$phrase' matches the rule" unless $phrase =~ /red|blue|white/;
or
say "'$phrase' matches the rule" if not $phrase =~ /red|blue|white/;
or many other variations.
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 6 years ago.
Improve this question
I have a groovy value like the following:
"hostname blah blah blah"
When that occurs, I want to keep the hostname, but delete everything after it including the first space.
I'm having difficulty with the regex. Any ideas?
Thanks!
To extract the hostname and/or a part of a string by a regular expression can be done in a straightforward way.
If the values are separated by whitespace then can just split the string by a space character and use the first part. Split() method takes in a regular expression as its first argument.
String s = "hostname blah blah blah"
String[] parts = s.split(' ', 2)
if (parts) {
println parts[0]
s = parts[0] // if want to replace the variale s with just the hostname
}
Here is an alternate example using a regular expression to pull out the hostname part of the string value. The first part of the regular expression (\S+) is looking for a sequence of non-whitespace characters. For a set of possible hostnames, a stricter expression could be something like (\w+(\.\w+)*).
import java.util.regex.Matcher
if (s =~ /^(\S+)\s/) {
println Matcher.lastMatcher.group(1)
}
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 7 years ago.
Improve this question
1) /abs/2-bhk-property-for-sale-in-builders-apartments-bang123asdxc/38070127?page=509
2) /vjr-apartments/private-k3zs0gdf
3) /dolphin-jasmine-apartments-navimumbai-approvals/psddp-3qfci22i
4) /kanaka-lakshmi-apartments-andra/private-67mwcdbe
What is the regex expression to match strings with 'apartment' but should not match 'private'?
i.e Should match 1) and 3) but not 2) and 4)
I wrote this regex .*?(-)(apartments)(?!\/private).* but it is not working.
You can use this regex:
-apartments(?!.*?/private)
(?!.*?/private) is negative lookahead that will fail the match if /private string comes after -apartments.
RegEx Demo
In some languages / needs to be escaped so use:
/-apartments(?!.*?\/private)/
This matches the line.
.*apartments(?!.*/private).*
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 9 years ago.
Improve this question
I want to be able to match all of the following strings to my regex below. It doesnt seem to be working. Any suggestions?
Strings to compare :
5878ce43aa3f1e1d713427d118115310 -1 Script Kiddie <perm>
f939f88b50fa5f0099b6751e7be27761 -1 Hacking <perm>
468f6634c5a9b00b5b3872dd6437143f 1356474103 Being Annoying <7day>
This is my perl code. It isnt working at the moment. Any suggestions?
my $bn_re = q{(.+?) (\d+) (.+?)};
If the first two fields are always without whitespace in them, you can use split to great effect, using the LIMIT option to only get three fields:
my ($str, $num, $other) = split ' ', $_, 3;
That is, assuming you read the file something like this:
while (<>) {
... # your code here
}
Also, this:
my $bn_re = q{(.+?) (\d+) (.+?)};
is not a regex. You may be confusing q() with qr(). You may also be confusing the functionality of
$str =~ $bn_re;
Which will automagically include the regex in a match operator m//. But you should use qr(). The q() operator does what the single quote does.
Also, you should be aware that .+? will match a single char if you allow it. As it does at the end of your "regex". At the end of your string, either do
... (.+)/ # matching greedily
... (.+?)$/ # using anchor to end of string
$bn_re =~ /[0-9a-z]+?\s[-0-9]+\s[\w\s]+?[<>a-z0-9]+?/i
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 8 years ago.
Improve this question
I have a big list of simple expressions (2Mb file). For example:
11.*;112.*;113.*;12.*;123.*
I need to remove unnecessary expressions and come up with this:
11.*;12.*
bash version would be appreciated. thanks in advance
Here is something in Perl that would work, provided the only wildcards in your pattern are of the form .*:
#!/usr/bin/perl
use strict;
use warnings;
my %terms;
{
local $/;
%terms = map {$_ => 1} split /;|\n/, <>;
}
foreach my $k1 (keys %terms)
{
foreach my $k2 (keys %terms)
{
if ($k1 ne $k2 and $k1 =~ /^$k2$/)
{
delete $terms{$k1};
last;
}
}
}
print join ';', keys %terms;
It accepts your file as a command line argument.
This works by comparing keys to each other. In each comparison, one key is treated as a string and the other key is evaluated as a regex. This takes advantage of the fact the .* matches anything--including the literal characters .*. Thus an expression that matches the literal string of another pattern will also match all strings that pattern would match.
It will work even if there are multiple .* terms in a single pattern. For example, it correctly determines that 1.*1.* matches everything that 11.* matches, deleting the latter.
However, this is kind of a hacky simplification and will not work if you introduce other regex patterns. There is no easy solution to this problem in general, because you would have to parse all patterns and figure out what each would match.