Regex negative match at end of string [duplicate] - regex

This question already has an answer here:
Find 'word' not followed by a certain character
(1 answer)
Closed 2 years ago.
I need an regex that match
"my dog", "My dog", "my &dog"
but not
"my dog#", "My dog#"
for search string "my dog". I have this expression at the moment by I have this:
reg_replace("/\b(my dog)\b/ui",'found','My dog');
But this obviously matches "my dog#" and not "My &dog". Any help would be appreciated.

my..?dog$
. = any Space
.? = 0 or 1 of any Space
$ = end of string
As not casesensitive this doesnt matter.
If you only have these, you can make a ".?" to get this one Dog with "&"
with end of string, you can simply get these other dogs you dont want out there.
Tested with regex101, if only these values are there, this is enough.
Flags = gsmi (Global, Multiline, singleline, insensitive)

Related

How can I remove a certain pattern from a string? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have this string like "682_2, 682_3, 682_4". (682 is a random number)
How can i get this string "2, 3, 4" using regex and ruby?
You can do this in ruby
input="682_2, 682_3, 682_4"
output = input.gsub(/\d+_/,"")
puts output
A simple regex could be
/_([0-9]+)$/ and in the match group of the result you will have 2 for 682_2 and 3 for 682_3
Ruby code snippet would be "64532_2".match(/_([0-9]+)/).captures[0]
you can use scan which returns an array containing the matches:
string_code.scan(/(?<=_)\d/)
(?<=_) tells to find a pattern that has a given pattern (_ in this case) before itself but wont capture that, it captures only \d. if it can have more than 1 digit like 682_13,682_33 then \d+ is necessary.

Regex function to find all and only 6 digit numeric string ignoring spaces if any any between [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have HTML source page as text file.
I need to read file and find out only those numeric strings which have 6 continous digits and can have a space in between those 6 digits
Eg
209 016 - should be come up in search result and as 400013(space removed)
209016 - should also come up in search and unaltered as 209016
any numeric string more then 6 digits long should not come up in search eg 20901677,209016#223, 29016,
I think this can be achieved by regex but I was not able to
A soln in regex is more desirable but anything else is also welcome
To match 6 digits with any number of spaces in between, you may use the following pattern:
\b(?:\d[ ]*?){6}\b
Or if you want to reject it when it's followed by an #, you may use:
\b(?:\d[ ]*?){6}\b(?!#)
Regex demo.
Then, you can use the replace method to remove the space characters.
Python example:
import re
regex = r"\b(?:\d[ ]*?){6}\b(?!#)"
test_str = ("209016 \n"
"209 016\n"
"20901677','209016#223', '29016")
matches = re.finditer(regex, test_str, re.MULTILINE)
for match in matches:
print (match.group().replace(" ", ""))
Output:
209016
209016
Try it online.
You can try the following regex:
\b(?<!#)\d(?:\s*\d){5}\b(?!#)
demo: https://regex101.com/r/ZCcDmF/2/
But note that you might have to modify your boundaries if you need to exclude more than the #. it will become something like:
\b(?<!#|other char I need to exclude|another one|...)\d(?:\s*\d){5}\b(?!#|other char I need to exclude|another one|...)
where you have to replace other char I need to exclude, another one,... by the characters.

Regex to match text, but not if contained in brackets [duplicate]

This question already has answers here:
Regex: match only outside parenthesis (so that the text isn't split within parenthesis)?
(2 answers)
Closed 3 years ago.
I could not find any practical way to do this, myself:
APPLE should be matched
APPLE APPLE should result in two matches
APPLE (APPLE) should result in one match
(BANANA APPLE) should result in no matches
()APPLE() should result in one match
The brackets can be separated from the wanted string by any length of text over multiple lines. Other brackets not containing the string can exist in any configuration.
EDIT None of the answers thus far (and thanks for them!) allow for newline characters between the brackets. Is this not a possibility?
Hope this will work fine
Regex: (?<!\()\bAPPLE\b(?![\w\s]*[\)])
\b a word boundary
(?![\w\s]*[\)]) Negatively lookahead for ) followed by words or spaces
(?<!\() Negatively lookbehind for (
RegexDemo
Try this Regex (if you don't need javascript regex):
(?<!\()\b\w+\b(?![\)])
See Explanation and try Demos
You may have some complex texts between (..\n..). So I suggest:
1- Add ) in beginning and ( to end of string! wait!
2- split your text with this regex /\)[^\(]*\b(\w+)\b[^\)]*\(/ or execute this regex, two similar ways!
3- you've extract texts out of parenthesis and you can use /\b\w+\b/ to match your words in out of parenthesis in Original text. /\bAPPLE\b/ is for APPLE.
for example:
var mytext = "Long text (for finding APPLE word and maybe replace\n"+
"by bold APPLE or something else!)\n"+
"So if APPLE APPLE and APPLE appear out of parenthesis\n"+
"they should convert to bold APPLE !" ;
mytext = ")" + mytext + "(" ;
var r= new RegExp(/\)[^\(]*\b(\w+)\b[^\)]*\(/g);
var res = mytext.match(r) ;
console.log(res);
//for each matched item in res,
// find APPLE and replace with new value
// by this regex: /\b\w+\b/g
in this case, you will able to ignore APPLE in "(BANANA APPLE what? no! it's not working :P should)" or more complex texts!

How to match a fixed string ending with a number using regex [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
I apologize in advance if this already has an answer but the best I could find relevant was:
Regex to test if a string ends with a number
Which is not exactly what I am looking for.
I am trying to figure a regex for a fixed string ending with number.
So lets say my fixed string is "This is Sparta"
So the regex would match for
"This is Sparta9"
"This is Sparta100"
"This is Sparta87"
"This is Sparta21"
"This is Sparta8"
But will not match for anything else before and after the string so
"This is Sparta7e" would not match
"Hi, This is Sparta7" would not match
"This is Sparta and 7" would not match
So basically a fixed String (CONSTANT) ending with number is the kind of regex I am looking for.
You just need your constant followed by at least one digit (\d+):
private static Pattern SPARTA_REGEX = Pattern.compile("This is Sparta(\\d)+");

Replacing surroundings of text in brackets when it occurs multiple times in a string [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I have a string containing LaTeX code, for example \emph{some words here} and I want to get Markdown syntax, for example,*some words here*. I tried:
s <- "some text in \\emph{italics} and some more ..."
pattern <- "\\\\emph\\{(.*)\\}"
gsub(pattern,"*\\1*", s)
> "some text in *italics* and some more ..."
However, I do not succeed at handling multiple occurences in one string.
s <- "some text in \\emph{italics} and some \\emph{more italics} and ..."
gsub(pattern,"*\\1*", s)
> "some text in *italics} and some \\emph{more italics* and ..."
I guess I need a non-greedy version which handles multiple occurrences, but I am not sure how to do it. Any ideas?
Use lazy ? quantifier like this.
Regex: \\\\emph{(.*?)}
Regex101 Demo