How to remove everything between two words in RegEx? - replace

I want to remove everthing between .com/ to ?utm
How can I do that?
I use Notepad++ and Kate.
https://www.forexample.com/cat.belt?utm2900&eav=AfbHJya2K7Mbg2mPWatN
https://www.forexample.com/cat.food?utm89748&eav=AfbHJya2K7Mbg2mPWatN
https://www.forexample.com/dog.necklace?utm25875&eav=AfbHJya2K7Mbg2mPWatN
https://www.forexample.com/dog.belt?utm25285&eav=AfbHJya2K7Mbg2mPWatN
https://www.forexample.com/dog.food?utm785844&eav=AfbHJya2K7Mbg2mPWatN
I tried to Google the solution, but nothing really works.

In that case, you could use (?:^.*?\.com\/)(.*?)(?:\?utm.*$) with $1 matching "cat.belt", "cat.food" etc.
In fact, the only change in comparison to #akash 's answer is that you invert the capturing and non-capturing groups.

try the pattern: (^.*?\.com\/)(?:.*?)(\?utm.*$)
substitution: $1$2
demo here: https://regex101.com/r/xChAme/2

Related

regex how to match whole words [duplicate]

I have the following line:
hshd household 8/29/2007 LB
I want to match anything that comes before the first space (whitespace). So, in this case, I want to get back
hshd
([^\s]+)
works
This should do it:
^\S*
Perhaps you could try ([^ ]+) .*, which should give you everything to the first blank in your first group.
Derived from the answer of #SilentGhost I would use:
^([\S]+)
Check out this interactive regexr.com page to see the result and explanation for the suggested solution.
for the entire line
^(\w+)\s+(\w+)\s+(\d+(?:\/\d+){2})\s+(\w+)$
I think, that will be good solution: /\S\w*/
I think, a word was created with more than one letters.
My suggestion is:
[^\s\s$]{2,}
^([^\s]+) use this it correctly matches only the first word you can test this using this link
https://regex101.com/
(^\S+)
This did the trick for me. (/gm)
You could test it in this

Regex substitution with Notepad++

I have a text file with several lines like these ones:
cd_cod_bus
nm_number_ex
cd_goal
And I want to get rid of the - and uppercase the following character using Notepad++ (I can also use other tool but if it doesn't get the problem more troublesome).
So I tried to get the characters with the following regex (?<=_)\w and replace it using \U\1\E\2 for the uppercasing trick but here is where my problems came. I think the regex is OK but once I click replace all I get this result:
cd_od_us
nm_umber_x
cd_oal
as you can see it is only deleting the match.
Do you know where the problem is?
Thanks.
The search regex has no capture groups, i.e. the \1 and \2 references in the replacement do not refer to anything.
Try this instead:
Search: _(\w)
Replace \U\1\E
There you have a capture group in the search part (the parenthesis around the \w) and the \1 in the replacement refers back to what was captured.
replace
_(.)
with
\U$1
will give you:
cdCodBus
nmNumberEx
cdGoal
and for your
I can also use other tool but if it doesn't get the problem more troublesome
I suggest you try vim.
Try this,
_(\w)
and replace with
\U\1
here's a screenshot

Regex to replace document.all

A simple (I hope) regex question:
I have several pages where I need to replace every document.all['string'] with document.getElementById('string').
I can use Visual Studio or Notepad++ to replace regular expressions, I just need the right ones.
Thanks for any assistance,
Guy
For notepad++:
search:
document\.all\[\'(.*)\'\]
replace with:
document.getElementById('\1')
Replace
document\.all\['(.*?)'\]
by
document.getElementById('$1')
The parentheses are used to identify a group. The $1 is used to print the value of the first group. The backslashes are used to escape special characters in regex. For the remnant it's pretty trivial.
Hope this helps.
Search for:
document\.all\['([^']+)'\]
Replace with:
document.getElementById('\1')

regular expression: match any word until first space

I have the following line:
hshd household 8/29/2007 LB
I want to match anything that comes before the first space (whitespace). So, in this case, I want to get back
hshd
([^\s]+)
works
This should do it:
^\S*
Perhaps you could try ([^ ]+) .*, which should give you everything to the first blank in your first group.
Derived from the answer of #SilentGhost I would use:
^([\S]+)
Check out this interactive regexr.com page to see the result and explanation for the suggested solution.
for the entire line
^(\w+)\s+(\w+)\s+(\d+(?:\/\d+){2})\s+(\w+)$
I think, that will be good solution: /\S\w*/
I think, a word was created with more than one letters.
My suggestion is:
[^\s\s$]{2,}
^([^\s]+) use this it correctly matches only the first word you can test this using this link
https://regex101.com/
(^\S+)
This did the trick for me. (/gm)
You could test it in this

RegEx to match two alternatives but nothing else

I need to capture either
\d+\.\d+
or
\d+
but nothing else.
For instance, "0.02", "1" and "0.50" should match positively. I noticed that I cannot simply use something like
[\d+\.\d+|\d+]
(\d+\.\d+|\d+)
should do the trick.
You can do either:
(\d+|\d+\.\d+)
or
(\d+(\.\d+)?)
but that creates a second capturing group. The more sophisticated version is:
(\d+(?:\.\d+)?)
That's called a non-capturing group.
By the way Regular Expression Info is a superb site for regular expression tutorials and information.
Or \d+(\.\d+)? if you find that easier to read :)