remove port number from list in notepad++ - replace

I have csv file that has the computer name in one column, and the same computer name with the port number on the second column. I want to compare that the name in both column 1 and 2 are the same. So I am trying to remove the :##### from the list. How do I do this?
I can't post a picture as I am too new here, but it looks like this:
ComputerName,ComputerName:18062
ComputerName2,ComputerName2:198099

Find ^((.*?),\2).*?$ and replace with \1. Use regular expression search mode, without . matches newline.
^ match start of line
Outer () define group 1.
(.*?), Match any character until , is found. Result is stored to group 2.
\2 Match same string again as in group 2.
.*?$ Match remaining characters until the end of line.
Entire line is replaced with group 1.

Related

How to match strings that are entirely composed of a predefined set of substrings with regex

How to match strings that are entirely composed of a predefined set of substrings. For example, I want to see if a string is composed of only the following allowed substrings:
,
034
140
201
In the case when my string is as follows:
034,201
The string is fully composed of the 'allowed' substrings, so I want to positively match it.
However, in the following string:
034,055,201
There is an additional 055, which is not in my 'allowed' substrings set. So I want to not match that string.
What regex would be capable of doing this?
Try this one:
^(034|201|140|,)+$
Here is a demo
Step by step:
^ begining of a line
(034|201|140|,) captures group with alternative possible matches
+ captured group appears one or more times
$ end of a line
This regex will match only your values and ensure that the line doesn't start or end with a comma. Only matches in group 0 if it is valid, the groups are non-matching.
^(?:034|140|201)(?:,(?:034|140|201))*$
^: start
(?:034|140|201): non-matching group for your set of items (no comma)
(?:,(?:034|140|201))*: non-matching group of a comma followed by non-matching group of values, 0 or more times
$: end

Notepad++ Search and Replace: delete 3 to 4 numbers after N in each row

I have a text file where almost all the lines start with the letter N followed by 3 or 4 numbers as below
N970 G2 X-1.0591 Y-1.7454 I0. J-.04
N980 G1 Y-1.7554
N990 X-1.0594 Y-1.7666
N1000 Z-.2187
N1010 Y-1.7566
How can I remove the N followed by the 3 or 4 numbers in Notepad++ to look like this? if i need to search twice (once for N### and then again for N####) that is fine also.
G2 X-1.0591 Y-1.7454 I0. J-.04
G1 Y-1.7554
X-1.0594 Y-1.7666
Z-.2187
Y-1.7566
the numbers go from 100-9990 in increments of 10 if that helps
You can use the following regex that should work for your case:
^N[0-9]+\s*(.*)
It will match every line that starts with a capital letter N immediately followed by one or more digits. Matched results will include a single group which will contain the text you are looking for.
Note that whitespaces between the N tags and the actual text will not be matched.
Try it out in this DEMO
Breakdown
^ # Assert position at the start of the line
N # Matches capital letter 'N' literally
[0-9]+ # Matches any digit between 1 and unlimited times
\s* # Matches whitespace between 0 and unlimited times
(.*) # The rest of the text you are looking for
Find/Replace
The regex will match each individual line so you can either select Find Next and then Replace and process your file one line at a time or you can choose Replace All to process the whole file at once.
Substitution line (Replace with:) line should just include the first group ($1) which represents the rest of your text with N-prefix tags trimmed.
Make sure that the Search Mode is set to Regular expression.

Select part of line in regular expression

I have this string:
#1#http://test.ir:8080/srvSC.svc#1#
#2#http://test.ir:8081/srvSC.svc#2#
#3#http://test.ir:8082/srvSC.svc#3#
#4#http://test.ir:8083/srvSC.svc#4#
#5#http://test.ir:8084/srvSC.svc#5#
#6#http://test.ir:8085/srvSC.svc#6#
I want to select all #1# #2# ... so in order to i wrote this expression : ^(^\#.\#) but it just select first line.How could i select first #.# and last of #.#?
You can use
^(#\d+#)(.+)\1$
That will capture the first #s in a group, repeat any characters, and then match the same characters that were matched in the first group. The string you want will be in the second captured group.
https://regex101.com/r/7Er0Ch/5

Add a new line of characters only after a line with specific characters

I would like to add a new line after every line that contains A1033 with a duplicate of that line except instead of A1033, I need it to say A3081.
So for example I need to go from this:
T9-P2818-L30:Count
T9-P8629-A1033-L999:Count
T9-P4960-V1000-P2818-L128:Foo
to this:
T9-P2818-L30:Count
T9-P8629-A1033-L999:Count
T9-P8629-A3081-L999:Count
T9-P4960-V1000-P2818-L128:Foo
The searching part is straight forward ^(A1033)$
but I'm not sure how to go about doing the rest using regex.
You may use
Find What: (.*)A1033(.*)
Replace With: $0\r\n$1A3081$2
Where:
(.*)A1033(.*) - matches and captures into Group 1 ($1) any 0+ chars as many as possible up to the last A1033 on the line, then matches A1033 and then captures the rest of the line into Group 2 ($2)
$0\r\n$1A3081$2 - inserts the whole match ($0), then appends a CRLF ending (use the one you need) and then appends Group 1 value, A3081, and then Group 2 value.
Here is the regex demo.
See the screenshot:

Regular Expression to Extract Text Bounded by '/'

I need to a regular expression to extract names from a GEDCOM file. The format is:
Fred Joseph /Smith/
Where the text bounded by the / is the surname and the Fred Joseph are the forenames. The complication is that the surname could be at any place in the text or may not be there at all. I need something that will extract the surname and capture everything else as the forenames.
This is as far as I have got and I have tried making groups optional with the ? qualifier but to no avail:
As you can see it has several problems: If the surname is missing nothing gets captured, the forename(s) sometimes have leading and trailing spaces, and I have 3 capture groups when I'd really like 2. Even better would be if the capture group for the surname didn't include the '/' characters.
Any help would be much appreciated.
For your last line, I'm not sure there is a way to join the group 1 with group 3 into a single group.
Here is my proposed solution. It doesn't capture spaces around forenames.
^(?:\h*([a-z\h]+\b)\h*)?(?:\/([a-z\h]+)\/)?(?:\h*([a-z\h]+\b)\h*)?$
To correctly match the names, care to use the insensitive flag, and if you test all lines at once, use multiline flag.
See the demo
Explanation
^ start of the line
(?:\h*([a-z\h]+\b)\h*)? first non-capturing group that matches 0 or 1 time:
\h* 0 or more horizontal spaces
([a-z\h]+\b) captures in a group letters and spaces, but stops at the end of the last word
\h* matches the possible remaining spaces without capturing
(?:\/([a-z\h]+)\/)? second non-capturing group that matches 0 or 1 time a name in a capturing group surrounded by slashes
(?:\h*([a-z\h]+\b)\h*)? third non-capturing group doing the same as first one, capturing the names in a third group.
$ end of the line
For your requirements
([A-z a-z /])+\w*
Sample
Hope this helps
(.\*?)\\/(.\*?)\\/(.\*)
Try this: ^([^/]*)(/[^/]+/)?([^/]*)$
This matches the following:
^ start of string (or with multiline modifier start of line)
([^/\n]*) anything other than / or new line zero or more times - this is captured as group 1
(/[^/\n]+/)? a single / followed by one or more non / or new line characters, then a single '/' character - this is captured as group 2, and is optional
([^/\n]*) anything other than / or new line zero or more times - this is captured as group 3
$ end of string (or with multiline modifier end of line)
You can see in action with your example text here: https://regex101.com/r/9kmKpy/1
To not capture the slashes you can add a non capturing group by adding ?: to the second set of brackets, and then adding another pair between the slashes:
^([^\/\n]*)(?:\/([^\/\n]+)\/)?([^\/\n]*)$
https://regex101.com/r/9kmKpy/2
I am not sure I follow what language is being used to extract the data, but based on what you have so far, you simply need to add '?':
(.*)(\/?.*\/?)(.*)
Not that this does not give you groupings for EACH name as some solutions will have multiple names in a single group
Edit:
Extending on Niitaku solution and looking at having each individual name in its own group, you could use:
^\s*(?:\/?([a-z]+)\/?)\s*(?:\/?([a-z]+)\/?)\s*(?:\/?([a-z]+)\/?)\s*$
As explained though, if using a language like ruby it would simply be:
ruby -pe '$_ = $_.scan(/\w+/)' file