Regexp groovy - regex

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: .*$

Related

Regex: extract characters from two patterns

I have the following string:
https://www.google.com/today/sunday/abcde2.hopeho.3345GETD?weatherType=RAOM&...
https://www.google.com/today/monday/jbkwe3.ho4eho.8495GETD?weatherType=WHTDSG&...
I'd like to extract jbkwe3.ho4eho.8495GETD or abcde2.hopeho.3345GETD. Anything between the {weekday}/ and the ?weatherType=.
I've tried (?<=sunday\/)$.*?(?=\?weatherType=) but it only works for the first line and I want to make it applicable to all strings regardless the value of {weekday}.
I tried (?<=\/.*\/)$.*?(?=\?weatherType=) but it didn't work. Could anyone familiar with Regex can lend some help? Thank you!
[Update]
I'm new to regex but I was experimenting it on sublime text editor via the "find" functionality which I think should be PCRE (according to this post)
Try this regex:
(?:sun|mon|tues|wednes|thurs|fri|satur)day\/\K[^?]+(?=\?weatherType)
Click for Demo
Link to Code
Explanation:
(?:sun|mon|tues|wednes|thurs|fri|satur)day - matches the day of a week i.e, sunday,monday,tuesday,wednesday,thursday,friday,saturday
\/ - matches /
\K - unmatches whatever has been matched so far and pretends that the match starts from the current position. This can be used for the PCRE.
[^?]+ - matches 1 or more occurences of any character that is not a ?
(?=\?weatherType) - the above subpattern[^?]+ will match all characters that are not ? until it reaches a position which is immediately followed by a ? followed by weatherType
To make the match case-insensitive, you can prepend the regex with (?i) as shown here
In the examples given, you actually only need to grab the characters between the last forward slash ("/") and the first question mark ("?").
You didn't mention what flavor regex (ie, PCRE, grep, Oracle, etc) you're using, and the actual syntax will vary depending on this, but in general, something like the following (Perl) replacement regex would handle the examples given:
s/.*\/([^?]*)\?.*/$1/gm
There are other (and more efficient) ways, but this will do the job.

Issues with Regex spaces and letters

I need help with REGEX please.
The 4 and 5 lignes are fine.
But the first, 2nd and 3rd I don't want the "t" be matched.
I tried a lot of things but didn't worked unfortunatly.
So here is the link of what I did :
www.regexr.com/39rtb
/!\ Click the link to see what I see :p
\bt^(t\u0027)|(\bt)
No I don't
just t'oyota
just test
No I don t
t
/!\ I need a code for the 2sd line and an other for the last ! (last line is a t with nothing else !)
Thanks
If you just want to match a single t, assuming your flavor lets you use this syntax, you can enforce the t to be surrounded by whitespace or nothing else like so:
(?<=\s|^)t(?=\s|$)
Demo
This makes use of lookaround expressions.
The splitted version (assuming (?m) - if it doesn't work then try to prepend (?m) to each pattern):
(?!^t$)(?<=\s|^)t(?=\s|$)
And:
^t$
But it doesn't make much sense to separate these cases using regexes. You'd be better off doing the check in your language.

Regular expression to split optional groups

Full string syntax is: "db:server:port"
Server and port are optional, i.e. can have partial strings, such as:
db
or
db:server
Trying to use:
(.*):?(.*)?:?(.*)?
selects the whole string
Please advise.
Give this one a shot:
([^:]*?):?([^:]*?):?([^:]*?)$
Not sure what language you're using, so it may not work.
Example: http://regex101.com/r/eQ6bF0
Note on the example it's set for a global/multiline match - beware that this will match across newlines if you don't use the correct modifier.
You didn't specify a language that I can see, so there may be different specific answers, but the basic problem is that .* will match a ":" character. That means the first term will suck the entire string in. I would use ([^:]*) instead of (.*).
You can try this:
([^:]+)(?::([^:]+)(?::([^:]+))?)?
I think this is what you're looking for:
(db|:server|:port)
will match any and all of these:
db:server:port
db
db:server
Working example:
http://regex101.com/r/rK1lI5

Regex match everything after question mark?

I have a feed in Yahoo Pipes and want to match everything after a question mark.
So far I've figured out how to match the question mark using..
\?
Now just to match everything that is after/follows the question mark.
\?(.*)
You want the content of the first capture group.
Try this:
\?(.*)
The parentheses are a capturing group that you can use to extract the part of the string you are interested in.
If the string can contain new lines you may have to use the "dot all" modifier to allow the dot to match the new line character. Whether or not you have to do this, and how to do this, depends on the language you are using. It appears that you forgot to mention the programming language you are using in your question.
Another alternative that you can use if your language supports fixed width lookbehind assertions is:
(?<=\?).*
With the positive lookbehind technique:
(?<=\?).*
(We're searching for a text preceded by a question mark here)
Input: derpderp?mystring blahbeh
Output: mystring blahbeh
Example
Basically the ?<= is a group construct, that requires the escaped question-mark, before any match can be made.
They perform really well, but not all implementations support them.
\?(.*)$
If you want to match all chars after "?" you can use a group to match any char, and you'd better use the "$" sign to indicate the end of line.
?(.*\n)+
With this you can get everything Even a new line
Check out this site: http://rubular.com/ Basically the site allows you to enter some example text (what you would be looking for on your site) and then as you build the regular expression it will highlight what is being matched in real time.
str.replace(/^.+?\"|^.|\".+/, '');
This is sometimes bad to use when you wanna select what else to remove between "" and you cannot use it more than twice in one string. All it does is select whatever is not in between "" and replace it with nothing.
Even for me it is a bit confusing, but ill try to explain it. ^.+? (not anything OPTIONAL) till first " then | Or/stop (still researching what it really means) till/at ^. has selected nothing until before the 2nd " using (| stop/at). And select all that comes after with .+.

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.