Regex to get a count of strings that match the pattern - regex

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=".*"

Related

Regex, extracting both word and number at the end of a string

I'm a beginner to regex and encountered a problem and didn't find a solution, So let's say I have a string ab123cd456, I'm trying to find a regex expression that would extract the text untill the last number (if any) and the number itself so the result of the extraction would be ["ab123cd", "456"]
extracting the end number is easily done by \d+$
but I am unable to make an expression to extract the ab123cd I've tried .*(?=\d+$) which extract ab123cd45 which is weird to me because + is a greedy expression
Please note that I want a single expression for the task
You need to have a non-greedy match as the first one:
import re
lines = ["abc12cd1234"]
for line in lines:
mre = re.match(r'(\w+?)(\d+)$', line)
if mre:
print mre.groups()
Will print:
('abc12cd', '1234')

regex to find a substring with key value pair

I want a regex for below scenario:
Input String:
"/eve/?tr=abcd&ay=123&test=123456789&e=event"
Output of regex should be (Search for attribute "test" and get the value against it: 123456789
I am using regex: [/test=([^\"]*)/, 1] but it returns string "123456789&e=event"
How to correct this regex?
You need to match everything up to the next &, not ":
[/test=([^&]*)/, 1]
Regexes aren't a magic wand that you wave at every programming problem that involves strings.
What language are you doing this in? Try to use existing tools instead of parsing your own with regexes. If you're using PHP, for example, use the parse_url function and then use explode to break apart the query string on = and &.
Try this regex !
You can extract anything before & .
&test=([^&]*)
I am also attach the output of the regex , you can verify it .

Regular Expression Match (get multiple stuff in a group)

I have trouble working on this regular expression.
Here is the string in one line, and I want to be able to extract the thing in the swatchColorList, specifically I want the word Natural Burlap, Navy, Red
What I have tried is '[(.*?)]' to get everything inside bracket, but what I really want is to do it in one line? is it possible, or do I need to do this in two steps?
Thanks
{"id":"1349306","categoryName":"Kids","imageSource":"7/optimized/8769127_fpx.tif","swatchColorList":[{"Natural Burlap":"8/optimized/8769128_fpx.tif"},{"Navy":"5/optimized/8748315_fpx.tif"},{"Red":"8/optimized/8748318_fpx.tif"}],"suppressColorSwatches":false,"primaryColor":"Natural Burlap","clickableSwatch":true,"selectedColorNameID":"Natural Burlap","moreColors":false,"suppressProductAttribute":false,"colorFamily":{"Natural Burlap":"Ivory/Cream"},"maxQuantity":6}
You can try this regex
(?<=[[,]\{\")[^"]+
If negative lookbehind is not supported, you can use
[[,]\{"([^"]+)
This will save needed word in group 1.
import json
str = '{"id":"1349306","categoryName":"Kids","imageSource":"7/optimized/8769127_fpx.tif","swatchColorList":[{"Natural Burlap":"8/optimized/8769128_fpx.tif"},{"Navy":"5/optimized/8748315_fpx.tif"},{"Red":"8/optimized/8748318_fpx.tif"}],"suppressColorSwatches":false,"primaryColor":"Natural Burlap","clickableSwatch":true,"selectedColorNameID":"Natural Burlap","moreColors":false,"suppressProductAttribute":false,"colorFamily":{"Natural Burlap":"Ivory/Cream"},"maxQuantity":6}'
obj = json.loads(str)
words = []
for thing in obj["swatchColorList"]:
for word in thing:
words.append(word)
print word
Output will be
Natural Burlap
Navy
Red
And words will be stored to words list. I realize this is not a regex but I want to discourage the use of regex on serialized object notations as regular expressions are not intended for the purpose of parsing strings with nested expressions.

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

. (dot) Regular expression

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