i have the folwing problem in regex
i want to get just the string from that pattern of regex
#10 12:00:00 - some text
#100 11:04:00 ! some text
#2 23:01:10 ? some text
this is pretty easy and the answer to that in regex will be
#[0-9]+ [0-9]{2}:[0-9]{2}:[0-9]{2} [-,!,?].*
but it gets much more complicated if I want to filter as well all the inputs i get with the time 00:00:00.
it is possible but it's really ugly is there any megical beautiful way to do this with a simple regex
You could use a negative lookahead (?!00:00:00) in the regex at the start
(?!00:00:00) fails when it matches the regex inside
https://www.regular-expressions.info/lookaround.html
Related
I am trying to remove a whole phrase from my regex(PCRE) matches
if given the following strings
test:test2:test3:test4:test5:1.0.department
test:test2:test3:test4:test5:1.0.foo.0.bar
user.0.display
"test:test2:test3:test4:test5:1.0".division
I want to write regex that will return:
.department
.foo.0.bar
user.0.display
.division
Now I thought a good way to do this would be to match everything and then remove test:test2:test3:test4:test5:1.0 and "test:test2:test3:test4:test5:1.0" but I am struggling to do this
I tried the following
\b(?!(test:test2:test3:test4:test5:1\.0)|("test:test2:test3:test4:test5:1\.0"))\b.*
but this seems to just remove the first tests from each and thats all. Could anyone help on where I am going wrong or a better approach maybe?
I suggest searching for the following pattern:
"?test:test2:test3:test4:test5:1\.0"?
and replacing with an empty string. See the regex demo and the regex graph:
The quotation marks on both ends are made optional with a ? (1 or 0 times) quantifier.
hey guys my regex skills suck and was looking for some help. I am using Notepad++ and am looking to easily replace everything between the following
example:
.dodo?ReplaceMe</dada>
Its in many lines i need to replace often so rather than hunting hightlighting and replacing i was hoping to score an easyier regex method that will save me time. The replace can include question mark if it makes it easier. Thanks in advance
You can match your pattern using \.dodo\?([^<]*)<\/dada>.
\.dodo\? matches .dodo? - you need to escape the . and ??
([^<]*) matches all characters aren't a <, ReplaceMe in this case, in a matching group
<\/dada> matches </dada> - you need to escape the /
View it in action here: https://regex101.com/r/bM2wE6/1
Use lookarounds:
(?<=\.dodo\?).*?(?=<\/dada>)
See it in action
I am having hard time learning regex and honestly I have no time at the moment.
I am looking for a regex expression that would match url route with query string
What I need is regex to match population?filter=nation of course where nation can be any string.
Based on my current regex knowledge I have also tried with regex expression /^population\/(?P<filterval>\d+)\/filter$/ to match population/nation/filter but this does not work.
Any suggestion and help is welcome.
This does match only your first query string format:
population\?filter=[\w]+[-_]?[\w]+
Addiotionally it allows for - and _ as bindings between words. If you know, that your string ends right there, you can also add an $ to the end to mark it so.
If you know that the nation is only alphabetical characters, yu can use the simplified version:
population\?filter=[\w]+
Demo
Within Notepad++, I want to replace many instances of the type ``string'' by \command{string} where string can be any string of characters. I am fairly close to what I want to achieve with:
Find: (?<=``)(.*?)(?='')
Replace: \\command{\1}
There is still a problem. With the regex code above, instead of \command{string} I get ``\command{string}'' and I am not sure why the `` and '' are not removed?
It is because you are using lookaround assertions. Lookaround (zero-width) assertions only assert that a position can be matched and do not "consume" any characters on the string. You can use the below regular expression.
Find: ``([^']+)''
Replace: \\command{\1}
You need to wrap everything into a capture group and use that. NP++ seems to not support lookahead/behind, but you dont need that for this specific case anyway:
``([^']+)'' -> \\command{\1}
This will make sure it does not match two commands (longest match) in something like:
run ``ls -l'' or ``ls -a''
I have this text
2014-01-30 10:15 some text here
2014-01-30 10:20 some other text here
I need a regex that matches a timestamp group in ISO 8601 format.
Required output:
2014-01-30T10:15Z
2014-01-30T10:20Z
With this REGEX I can't get what I want, replace the space with 'T' and append a 'Z at the end.
^(?<timestamp>\S+ \S+)
Does anyone know how to solve this problem?
--- UPDATE ---
BTW, I'm using http://rubular.com/ to test my regex
You could perhaps modify your current regex a bit to:
^(\S+) (\S+).*
And replace with $1T$2Z
regex101 demo
\d{4}-\d{2}-\d{2} \d{2}:\d{2} will match the required format – validation is another story though (if you need it).
You can do something like if (regex match) { replace " " with "T"; append "Z" }
If this doesn't help you or it is unclear it is because your question was vague.
Edit: you didn't specify what language you're writing this in. That is how you would do your replacements.
In php:
preg_replace('/^(\S+) (\S+).*/', "$1T$2Z", $str);
In perl:
$str =~ s/^(\S+) (\S+).*/$1T$2Z/;
In notepad++
Find what: ^(\S+) (\S+).*
Replace with: $1T$2Z
With:
(\d{4}-\d{2}-\d{2})( \d{2}:\d{2} )(?:.*)
You can capture 2014-01-30 10:15 in groups (and ignore the text in another group).
Then you use the second group (10:15) to add 'T' at the beginning and 'Z' at the end.
See demo at:
http://rubular.com/r/4icGfcIixa
Regex is a bit different from language to language, it could help if you told us what language you are using.
For example, in javascript, you can do something like this:
"2014-01-30 10:15 some text here".replace(/(\d{4}-\d{2}-\d{2})\s(\d{2}:\d{2})\s?.*/,"$1T$2Z")
Where the string can be a variable.
If you have a multiple line text them you should add a g at the end of the regex:
"2014-01-30 10:15 some text here\n2014-01-30 10:20 some other text here".replace(/.*(\d{4}-\d{2}-\d{2})\s(\d{2}:\d{2})\s?.*/g,"$1T$2Z")