. (dot) Regular expression - regex

I would like to add delimters to a .txt file.
Each line has the same amount of characters; and I know where the splits should happen.
For example,
MyNameIsHarry
I would like to transform the file to look like this instead:
My|Name|Is|Harry
I am on notepad++ using Regular Expression, and I can do this:
(..)(....)(..)(.....)
Replace with
\1|\2|\3|\4
Is there a more efficient way I can write this regular expression? Would i have to use 100 "." (dots) if there was a split of 100 characters?
Many thanks for your help!

http://www.regular-expressions.info/reference.html at your service!
You can use (.{100}) if you expect exactly 100.
as stated in the reference:
{n} where n is an integer >= 1
Repeats the previous item exactly n times.
Example: a{3} matches aaa

If the text is all in the same format as your example you could just use:
Find what : ([a-z])([A-Z])
Replace with : \1|\2
Make sure Match case and Regular expression are checked
Replace All

Related

Regex to get a count of strings that match the pattern

I want to count the number of occurrences of the below format of string in notepad++.
name="*" lastName="*"
(* represents it can be any name/lastName)
I am using the following regex
/name="([A-Z])\w" lastName="([A-Z])\w"
I am not able to figure out how to give hard coded values as a part of regex.
Notepad ++ solution is: name=".*" lastName=".*"

RegEx in Notepad++ to find a wild character and replace the whole word

I have a test file with number values as below:
32405494
32405495
32405496
32407498
Using Notepad++, what I am trying to achieve here is to search the first 4 digits using regular expression and replace the whole number with G3E_STYLERULE_SEQ.NEXTVAL
I am able to find these values using 3240*. My question is, how do I replace the whole number with G3E_STYLERULE_SEQ.NEXTVAL?
When I am click the Replace All button, I get the following output:
G3E_STYLERULE_SEQ.NEXTVAL5494
G3E_STYLERULE_SEQ.NEXTVAL5495
G3E_STYLERULE_SEQ.NEXTVAL5496
G3E_STYLERULE_SEQ.NEXTVAL7498
However, I am expecting the following:
G3E_STYLERULE_SEQ.NEXTVAL
G3E_STYLERULE_SEQ.NEXTVAL
G3E_STYLERULE_SEQ.NEXTVAL
G3E_STYLERULE_SEQ.NEXTVAL
Any ideas to achieve this? Is it even possible through Notepad++? Are there any other text editors which I can use to achieve this?
Use something like this:
3240.*
. is the wildcard character in regex and * means that the previous character is to be repeated 0 or more times (your current regex actually matches 324 and then 0 which appears 0 or more times).
3240.* will therefore match 3240 and any other following characters.
You might also want to add a line anchor:
^3240.*
So that you don't replace numbers having 3240 in the middle too.
in notepad++, you can use this regex:
^3240\d+
it will match the four digits you're searching at the beginning of your string followed by any digit.
Try this -
Search this - ^3240\d*$
Replace with- G3E_STYLERULE_SEQ.NEXTVAL

Regex to increase a string by one only (consecutive numbers)

I use Notepad++, and I need a regular expression to match consecutive numbers only
Example:
verses 3-4
verses 11-12
verses 26-27
so the regex finds these matches, and not lines like: verses 3-9, or verses 26-32.. etc.
I use the \d+, but don't know how to increase the same one by just one.
Regular expressions match text, not numbers. Therefore, you can't do this with a regex alone (unless you spell out all possible combinations). You need a scripting language that converts the matched texts to integers and compares those.
For example, in Python:
for potentialmatch in re.finditer(r"(\d+)-(\d+)", mytext):
if int(potentialmatch.group(1)) + 1 == int(potentialmatch.group(2)):
# Match found
You cannot do that, short of using a regex that encompasses all such options.
You can generate one, though:
(1..99 | %{"$_-$($_+1)"}) -join '|'
in PowerShell yields a regex that will match everything from 1-2 to 99-100.

Regular Expression in Vim that will count 1 or colon

I am rather new to using vim with regular expressions and I need to count specific entries in a .csv file The entries are in this form:
9,1,8-Mar-11,high,A2,mid,500,1000,0.143494345,0.153521446,1121.386992,409.6833333,,
9,2,8-Mar-11,high,A2,mid,500,1000,0.180015537,0.256840072,1190.977918,420.8229933,1,
9,3,8-Mar-11,high,A2,mid,500,1000,0.250273568,0.16378268,1061.417761,419.1692065,,1
I need to count the number of 8-Mar-11, A2 conditions which have either ,, or ,1 (,|1) or (,|1),1 on the end of the lines.
here is the regular expression that i use in vim to get some count data:
:%s/.*8-Mar-11.*A2.*,,1$//gn
What I would like to know is there a way to use either in vim? like:
:%s/.*8-Mar-11.*A2.*,1\(,|1\)//gn
Any advice or help is greatly appreciated!
VIM regexes are weird and confusing. You have to escape the | as \| for or
When you're composing a regular expression which is searching for one character or another, alternation using pipes is overkill. Use a character class instead:
:%s/.*8-Mar-11.*A2.*,1[,1]//gn

What regular expression can I use to find the Nᵗʰ entry in a comma-separated list?

I need a regular expression that can be used to find the Nth entry in a comma-separated list.
For example, say this list looks like this:
abc,def,4322,mail#mailinator.com,3321,alpha-beta,43
...and I wanted to find the value of the 7th entry (alpha-beta).
My first thought would not be to use a regular expression, but to use something that splits the string into an array on the comma, but since you asked for a regex.
most regexes allow you to specify a minimum or maximum match, so something like this would probably work.
/(?:[^\,]*,){5}([^,]*)/
This is intended to match any number of character that are not a comma followed by a comma six times exactly (?:[^,]*,){5} - the ?: says to not capture - and then to match and capture any number of characters that are not a comma ([^,]+). You want to use the first capture group.
Let me know if you need more info.
EDIT: I edited the above to not capture the first part of the string. This regex works in C# and Ruby.
You could use something like:
([^,]*,){$m}([^,]*),
As a starting point. (Replace $m with the value of (n-1).) The content would be in capture group 2. This doesn't handle things like lists of size n, but that's just a matter of making the appropriate modifications for your situation.
#list = split /,/ => $string;
$it = $list[6];
or just
$it = (split /,/ => $string)[6];
Beats writing a pattern with a {6} in it every time.