regex how to match whole words [duplicate] - regex

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

Related

How to remove everything between two words in RegEx?

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

regex get everything after dot

Need a help with a simple regex.
I need to remove everything before the first dot (including the dot)
Example:
Text-To-remove.Text.ToKeep.123.com
Should be:
Text.ToKeep.123.com
I tried \.(.*) but it keeps the first dot:
.Text.ToKeep.123.com
Please advice
One approach uses a positive lookbehind:
(?<=\.).*$
Demo

Regexp groovy

I need a regexp to find strings that start with a specific word then comes colon and whitespace for example
"ErrorID: blabla"
Please help. :(
This should work fine:
^(\w+): (.+)$
First match group will give the first word (e.g. ErrorID), second the rest (e.g. blabla).
Exact implementation would depend on the programming language you use.
This should do what you want:
^ErrorID: .*$

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

Match last word after /

so, i have some kind of intern urls: for example "/img/pic/Image1.jpg" or "/pic/Image1.jpg" or just "Image1.jpg", and i need to match this "Image1.jpg" in other words i want to match last character sequence after / or if there are no / than just character sequence. Thank you in advance!
.*/(.*) won't work if there are no /s.
([^/]*)$ should work whether there are or aren't.
Actually you don't need regexp for this.
s="this/is/a/test"
s.substr(s.lastIndexOf("/")+1)
=> test
and it also works fine for strings without any / because then lastIndexOf returns -1.
s="hest"
s.substr(s.lastIndexOf("/")+1)
=> hest
.*/([^/]*)
The capturing group matches the last sequence after /.
The following expression would do the trick:
/([\w\d._-]*)$
Or even easier (but i think this has also been posted below before me)
([^/]+)$
A simple regex that I have tested:
\w+(.)\w+$
Here is a good site you can test it on: http://rubular.com/
In Ruby You would write
([^\/]*)$
Regexps in Ruby are quite universal and You can test them live here: http://rubular.com/
By the way: maybe there is other solution that not involves regexps? E.g File.basenam(path) (Ruby again)
Edit: profjim has posted it earlier.
I noticed you said in your comments you're using javascript. You don't actually need a regex for this and I always think it's nice to have an alternative to using regex.
var str = "/pic/Image1.jpg";
str.split("/").pop();
// example:
alert("/pic/Image1.jpg".split("/").pop()); // alerts "Image1.jpg"
alert("Image2.jpg".split("/").pop()); // alerts "Image2.jpg"
Something like .*/(.*)$ (details depend on whether we're talking about Perl, or some other dialect of regular expressions)
First .* matches everything (including slashes). Then there's one slash, then there's .* that matches everything from that slash to the end (that is $).
The * operates greedily from left to right, which means that when you have multiple slashes, the first .* will match all but the last one.