Regular expression Regex to extract a string [closed] - regex

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 4 years ago.
Improve this question
Please can somebody help me, I`m new to regex and have no idea how to do this!.
I`m trying to extract from a list which looks like this...
Joe-Age23-46737-251.aspx
Tim-Age18-46909-451.aspx
Roger-Age41-59768-251.aspx
What I want is this...
46737-251.aspx
46909-451.aspx
59768-251.aspx
so basically anything after the second to last hyphen.
Cheers

Let's translate "everything after the second-to-last hyphen" into regex:
(?<=-)[^-]*-[^-]*$
Explanation:
(?<=-) # Assert starting position right after a hyphen
[^-]* # Match zero or more characters except hyphens
- # Match a single hyphen
[^-]* # see above
$ # until end of string.
Test it live on regex101.com.

Step1 : Split the string on the basis of hyphen(-) . You will get array of strings.
Step2 : extract the second , fifth and eighth
and so on( incremented by 3 ).
Step3 : concatinate all the strings formed in step2.

Related

RegEx or PowerShell to remove repeat characters in sequence [closed]

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 4 years ago.
Improve this question
Writing a script to convert a Windows host file into a CSV. Used RegEx to get to this stage:
1.1.1.1,,server1,,,
2.2.2.2,,server2
3.3.3.3,,server3
4.5.6.7,,server4,,server5,,server6,,server7,,
8.8.8.8,,server8
9.9.9.9,server9
I need some RegEx that can remove the duplicate commas (in sequence) so it would look like this:
1.1.1.1,server1,
2.2.2.2,server2
3.3.3.3,server3
4.5.6.7,server4,server5,server6,server7,
8.8.8.8,server8
9.9.9.9,server9
Will also need to remove the comma at the end of each line (if there is one) but think this will be simpler to do.
The regex for your first task of removing duplicate commas was already provided in the comments above, but if you also want to remove trailing commas at the end of the line, you can use this to solve both problems at once:
(?m),(?=,|$)
Explanation:
(?m) # turn on multiline mode ($ matches end-of-line, not just end-of-string)
, # Match a comma
(?= # only if followed by
, # another comma
| # or
$ # the end of the string.
) # End of lookahead assertion
Test it live on regex101.com.

Regex: Matching only groups that have a specific word embedded [closed]

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 cannot figure out how to match only on groups that contain a certain word ('test' for example below). It is a big text file and the groups start with a line 'Group x' and include text with an empty line separation to the next group. I think I need to use lookaheads and lookbehinds but don't know how. I can use vb.net for this but trying to test out different expressions in the regex testers and can't get anywhere.
Group 1
adfdf
dd test ddfdf
dfdfadf
Group 2
ddfadfa
Group 3
add test
adfdff
Group 4
adfdf
Expected 2 matches:
Group 1
adfdf
dd test ddfdf
dfdfadf
Group 3
add test
adfdff
Start your pattern with ^Group \d+$ and end with (?:^$|\Z). In the middle match test but not preceeded by an empty line $(?:.(?!^$) (see Regular expression to match a line that doesn't contain a word? for details on how the latter works). Don't forget the m and s modifiers:
^Group \d+$(?:.(?!^$))*?test.*?(?:^$|\Z)
Demo: https://regex101.com/r/kM9qB3/2

How to match a line which should not contain a word after a matched word [closed]

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).*

Regex for matching just the first occurrence of a comma in each line [closed]

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
What does the regex look like for matching only the first instance of a comma, and nothing but that comma?
I have tried things like ,{1} and I think it has something to do with non-greedy qualifiers like this ,(.*?), but I have had no success.
I'm using Notepad++ to try to convert code from another language to JavaScript. I want to turn the first comma into a colon. It looks like this:
'TJ', 'Tajikistan' ,
'TZ', 'Tanzania' ,
'TH', 'Thailand' ,
'TL', 'Timor-Leste' ,
'TG', 'Togo' ,
'TK', 'Tokelau' ,
'TO', 'Tongo' ,
'TT', 'Trinidad and Tobago' ,
Find what: /,/
Replace with: :
0 occurrences were replaced
What you can do is, instead of just replacing the first comma with a colon, you can automatically replace the comma and everything after it with the colon plus everything that was after the comma. (For example, in 'TZ', 'Tanzania' ,, this approach would replace , 'Tanzania' , with : 'Tanzania' ,.) After that, since the rest of the line has already undergone replacement, Notepad++ doesn't re-examine it to see whether it contains a comma.
The way you do that is by using a capture group, which lets the replacement-string incorporate part of what the regex matched.
Specifically, you would replace this ("Find what"):
,(.*)
meaning "a comma (,), plus zero or more characters (.*), and capture the latter (())", with this ("Replace with"):
:$1
meaning "a colon (:), plus whatever was captured by the first capture group ($1)".

Perl Regex Ban Log [closed]

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