I have this regex
(?<=TG00).*?(?=#)
which extracts all strings between TG00 and #. Demo: https://regex101.com/r/04oqua/1
Now, from above results I want to extract only the string which contains TG40 155963. How can I do it?
Try this pattern:
TG00[^#]*TG40 155963[^#]*#
This pattern just says to find the string TG40 155963 in between TG00 and an ending #. For the sample data in your demo there were 3 matches.
Demo
For some reason appending .*? to your lookbehind results in engine error, but works fine with lookahead. Regex below does not match your text exactly, but it does extract it via capture group.
(?<=TG00).*?(TG40 155963)(?=.*?#)
You can use this regex with a lookahead and negated character class:
(?<=TG00)(?=[^#]*TG40 155963)[^#]+(?=#)
RegEx Demo
RegEx Explanation:
(?<=TG00): Assert that we have TG00 at previous position
(?=[^#]*TG40 155963): Lookahead to assert we have string TG40 155963 after 0 or more non-# characters, ahead
[^#]+: Match 1+ non-# characters
Related
So I am trying to make a regex match for strings of the form:
"catalog.schema.'tablename'" .
The output I am looking for is just catalog.schema.'tablename' leaving out the quotes at the end position.
Can anyone help me out
I tried to do it with the expression
/(?!^|.$)+[^\s]/ which leaves out the end quotes but matches each character.
So I modified it to /(?!^|.$)+[^\s]+/g . This matches the whole sentence but doesn't ignore the end quote.
Depends on the data arround your string and quotationmarks may be within the string.
Why not just this: "(.*?)"
https://regex101.com/r/oaS8o0/1
To answer the question in the title you might simply use:
^.(.*)?.$
https://regex101.com/r/FxJgtW/1
You can just use
(?<=.).+(?=.)
Or, if you cannot use lookbehind:
(?!^).+(?!$)
See the regex demo #1 and regex demo #2.
Since . matches any char other than line break chars, the patterns just match any strings without their start and end chars.
If you don't want to match the first and the last character, you can just use a capture group instead of lookarounds and use the group 1 value.
The first . matches the first of (any) characters, the (.+) is a capture group that matches 1 or more characters, and the . at the end matches the last character of the string.
.(.+).
Regex demo
Or to get the text between the double quotes at the start and the end of the string using a negated character class and a capture group:
^"([^"]+)"$
Regex demo
For example we have a string:
asd/asd/asd/asd/1#s_
I need to match this part: /asd/1#s_ or asd/1#s_
How is it possible to do with plain regex?
I've tried negative lookahead like this
But it didn't work
\/(?:.(?!\/))?(asd)(\/(([\W\d\w]){1,})|)$
it matches this '/asd/asd/asd/asd/asd/asd/1#s_'
from this 'prefix/asd/asd/asd/asd/asd/asd/1#s_'
and I need to match '/asd/1#s_' without all preceding /asd/'s
Match should work with plain regex
Without any helper functions of any programming language
https://regexr.com/
I use this site to check if regex matches or not
here's the possible strings:
prefix/asd/asd/asd/1#s
prefix/asd/asd/asd/1s#
prefix/asd/asd/asd/s1#
prefix/asd/asd/asd/s#1
prefix/asd/asd/asd/#1s
prefix/asd/asd/asd/#s1
and asd part could be replaced with any word like
prefix/a1sd/a1sd/a1sd/1#s
prefix/a1sd/a1sd/a1sd/1s#
...
So I need to match last repeating part with everything to the right
And everything to the right could be character, not character, digit, in any order
A more complicated string example:
prefix/a1sd/a1sd/a1sd/1s#/ds/dsse/a1sd/22$$#!/123/321/asd
this should match that part:
/a1sd/22$$#!/123/321/asd
If you want the match only, you can use \K to reset the match buffer right before the parts that you want to match:
^.*\K/a\d?sd/\S+
The pattern will match
^ Start of string
.* Match any char except a newline until end of the line
\K Forget what is matched until now
/a\d?sd/ match a, optional digits and sd between forward slashes
\S+ Match 1+ non whitespace chars
See a regex demo
I have this regexp:
^[a-z0-9]+([.\-][a-z0-9]+)*$
I need exclude from match only one word "www".
I tried the negative lookahead but without a success.
Use a negative lookahead like this:
^(?!www$)[a-z0-9]+([.-][a-z0-9]+)*$
^^^^^^^^
This will not match a string equal to www.
See the regex demo
If you want to fail a match with strings that contain -www- or .www., use
^(?!.*\bwww\b)[a-z0-9]+([.-][a-z0-9]+)*$
See another regex demo. This pattern contains a (?!.*\bwww\b) lookahead that fails the whole match if there is a www somewhere inside the string and it has no digits or letters round it due to \b word boundaries.
I am using the following regex for detecting negative numbers:
([-]([0-9]*\.[0-9]+|[0-9]+))
But I want to skip the matches which are followed by $.
If i use the folowing regex:
([-]([0-9]*\.[0-9]+|[0-9]+)[^\$])
It will match correctly the positions but will include the following character.
For example in expression:
-0.6+3 - 3.0$
it will match:
-0.6+
I want to match only
-0.6
([-]([0-9]*\.[0-9]+|[0-9]+)(?!\$)
You need a negative lookahead here which will not consume and only make an assertion.
Remove the $ from the group:
([-]([0-9]*\.[0-9]+|[0-9]+))[^\$]
You could use this simplified regex:
(-[0-9]+(?:\.[0-9]+)?)(?!\$)
You can use the regex from Regular-Expressions.info with just minus at the beginning, and a \b added at the end in order to stop before any non-word character:
[-][0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\b
This regex also captures the numbers with exponent part.
See demo
I need to write a RegEx to match the "1-234-5678" string if there are no dash characters around it.
I have the following RegEx:
\b\d\-\d{3}\-\d{4}\b
Now this works fine and matches "1-234-5678" correctly in the strings below:
text 1-234-5678 text
111 1-234-5678 1212
The RegEx also correctly NOT matches "1-234-5678" in the strings below:
text1-234-5678text
1111-234-56781212
But the problem is that it also matches in the following strings:
text-1-234-5678-text
111-1-234-5678-1212
It's because \b matches before and after the dashes.
How can I eliminate matches if there's a dash in front or after the data?
Use a negative lookbehind and negative lookahead to check whether the above mentioned format is not preceded and followed by a - symbol,
(?<!-)\b\d\-\d{3}\-\d{4}\b(?!-)
DEMO