This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to find a regex which searches for every numbers if a keyword exists in the paragraph.
For example, if my keyword is something, with this paragraph:
20
30
abc
40
def
something
my regex should get 20, 30 and 40. But for this one:
50
60
xyz
it should get nothing.
Can you guys help me out to find a good regex. Thank you so much! I'm using PCRE
You can use this regex in single line (DOTALL) mode with a lookahead assertion:
(?s)\b\d+(?=.*\bsomething\b)
It will match numbers only when there is a word something ahead in input.
RegEx Demo
RegEx Details:
(?s): Enable single line mode so that dot also matches newlines
\b: Match a word boundary
\d+: Match 1+ digits
(?=.*\bsomething\b): Positive lookahead to assert that we have a word something ahead of us from current position
Related
Using PCRE, I want to capture only and all digits in a line which follows a line in which a certain string appears. Say the string is "STRING99". Example:
car string99 house 45b
22 dog 1 cat
women 6 man
In this case, the desired result is:
221
As asked a similar question some time ago, however, back then trying to capture the numbers in the SAME line where the string appears ( Regex (PCRE): Match all digits conditional upon presence of a string ). While the question is similar, I don't think the answer, if there is one at all, will be similar. The approach using the newline anchor ^ does not work in this case.
I am looking for a single regular expression without any other programming code. It would be easy to accomplish with two consecutive regex operations, but this not what I'm looking for.
Maybe you could try:
(?:\bstring99\b.*?\n|\G(?!^))[^\d\n]*\K\d
See the online demo
(?: - Open non-capture group:
\bstring99\b - Literally match "string99" between word-boundaries.
.*?\n - Lazy match up to (including) nearest newline character.
| - Or:
\G(?!^) - Asserts position at the end of the previous match but prevent it to be the start of the string for the first match using a negative lookahead.
) - Close non-capture group.
[^\d\n]* - Match 0+ non-digit/newline characters.
\K - Resets the starting point of the reported match.
\d - Match a digit.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to match (extract) any character/symbol at the 3rd position of a string using regex. And no, I can't use substrings for this scenario. Below are examples I want matched:
ABCDEF => C
123456 => 3
A B C => B
I'm also being guaranteed to have a string of more than 10 characters so I don't have to worry about being less that 3 characters.
Any help would be appreciated.
You may use this regex with a negative lookbehind:
(?<=^..).
RegEx Demo
RegEx Details:
(?<=^..): Lookbehind assertion to match any 2 characters at line start
.: Match character at 3rd position
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
I have different paths in a column like below:
'E:/R_Process/R_Input_UDM//Greater Europe/CEW Hub/Austria/Toothpaste/Variant Data/AUSTRIA_SHG_VARIANT_8.csv'
'E:/R_Process/R_Input_UDM//Greater Europe/CEW Hub/Austria/ManualTB/Variant Data/AUSTRIA_SHG_VARIANT_7.csv'
'E:/R_Process/R_Input_UDM//Greater Europe/CEW Hub/Austria/MouthWash/Variant Data/AUSTRIA_SHG_VARIANT_9.csv'
I want Toothpaste from 1st path, ManualTB from 2nd, and MouthWash from 3rd.
There are many more paths around 30 every from every paths I want the word from 7th slash.
How can I do this using regex?
You can use the following regex:
(?:\/([^\/\n]*)){7}
Check Regex Demo
Required keywords would be in capture group 1
You can match any optional characters followed by a slash and capture the word following it by using this regex,
(?:.*?\/){7}(\w+)
Explanation:
(?:.*?\/){7} --> Non capture group matching any optional characters followed by / seven times
(\w+) --> Capture the following word in group 1
Demo
Let me know if this works fine for you.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a huge file and need to replace some strings, the problem is that they are dynamic but always follow a pattern:
year[4 digits number]/month[2 digits number]/timestamp[8 digits number]/file[random string ending with extension]
Some examples:
2017/07/24204301/a-4.png
2017/07/24204318/a-5-e1501986401369.png
2017/11/24211223/questao10branca-172x300.png
I need to remove the timestamp on all occurrences, then the above example would become:
2017/07/a-4.png
2017/07/a-5-e1501986401369.png
2017/11/questao10branca-172x300.png
How can I achieve this using Regexp and Visual Studio Code?
Given the examples you presented, there are a couple of regular expressions that will work for you.
See regex in use here
/\d{8}(?=/)
/ Match this literally
\d{8} Match any digit exactly 8 times
(?=/) Positive lookahead ensuring what follows is a literal /
See regex in use here
(?<=^\d{4}/\d{2}/)\d{8}/
(?<=^\d{4}/\d{2}/) Negative lookbehind ensuring what precedes is the following:
^ Assert position at the start of the string
\d{4} Match any digit exactly 4 times
/ Match this literally
\d{2} Match any digit exactly twice
/ Match this literally
\d{8} Match any digit exactly 8 times
/ Match this literally
This question already has answers here:
How can I use 'Not Like' operator in MongoDB
(2 answers)
Closed 2 years ago.
My mongo documents all contain a field called templateName. There are a few documents that contain the value: a_SystemDefaultTemplate, b_SystemDefaultTemplate, c_SystemDefaultTemplate etc.
I would like to find those documents whose templateName does not end with (or contain) SystemDefaultTemplate
I know it can be done using the $not operator like so:
db.collection.find({templateName: {$not: /.*SystemDefaultTemplate$/}})
But how do I do the same using regex?
I have tried the below but it does not seem to work.
db.collection.find({templateName: {$regex: "^(.*SystemDefaultTemplate$)"}})
try with negative look ahead ( meaning it should not contain the mentioned phrase)
db.collection.find({templateName: {$regex: "^(?!SystemDefaultTemplate$)"}})
?! is negative look ahead. And here is some explanation about it from http://rexegg.com/regex-disambiguation.html#lookarounds
"Negative Lookahead After the Match: \d+(?!\d| dollars)
Sample Match: 100 in 100 pesos
Explanation: \d+ matches 100, then the negative lookahead (?!\d| dollars) asserts that at that position in the string, what immediately follows is neither a digit nor the characters " dollars"
Negative Lookahead Before the Match: (?!\d+ dollars)\d+
Sample Match: 100 in 100 pesos
Explanation: The negative lookahead (?!\d+ dollars) asserts that at the current position in the string, what follows is not digits then the characters " dollars". If the assertion succeeds, the engine matches the digits with \d+."