I have had my head scratching over a simple, but complicated problem for me. And have been trying to find solution as well as doing hit and trials since 5 hours, unfortunately not able to solve.
There is a string which is like "Dept #809 something something something and so on", I need to exclude "Dept #809", and in place of 809 it could be any number 1 to 3 characters long. I am able to match this string using this regex /^(Dept #(\d{1,3}))/, but I simply want to exclude this. Have done most of the things, but not able to do :(.
Please help me out!
If you cannot use plain JavaScript code and depend on a single regex pattern to extract the part of string you need, use a regex to match the beginning of your input, and match and capture the rest of the string.
You may do it with
/^Dept #\d+(.*)/
Or - if there may be line breaks:
/^Dept #\d+([\s\S]*)/
See the regex demo.
In both case, Dept #<DIGITS> is matched at the string start, and the rest is captured into Group 1 that you should be able to access after the match is found.
Related
I read from somewhere that Datastudio use little bit different Regular Expression from other places: that it uses RE2. I, however, manage to find a site to test for RE2 regex and able to get it running, but it was not working on Data studio.
I have this URL I wanted to extract:
/marketing/news-717777
/finance/news-123456?asdasdasd_asdad
I wanted the regex to extract the word with dash and number. "news-******".
The result would be like this
news-717777
news-123456
I cannot seem to get it to work on data studio. The code that I have tried are the following:
(news-).*(?=\?)|(news-).*
(news-).*(?=\\?)|(news-).*
(news-.*?)\?
(news-.*).*(?=\?)
The closest I get is to get news with number"news-***", but I cannot remove the "?" that comes after. Anyone has any ideas on this? Thank you in advance.
You can use several solutions here.
Solution 1: matching digits after a specific string (here, news-)
(news-[0-9]+)
See the regex demo, [0-9]+ matches one or more digits.
Solution 2: If there can be any char other than ? after news-, if there can be chars other than digits, you can use
(news-[^?]+)
See this regex demo, where [^?]+ matches one or more chars other than a ? char.
I have text that looks something like this (but much uglier of course):
On 9/02/2019, 9/03/2019 and 9/05/2019 this thing happened
and I would like to regex capture anything related to the date
On 9/02/2019, 9/03/2019 and 9/05/2019
Using regex
([a-zA-Z ]{0,5}(?:\d+\/)+\d+\s*(?:,|and|\s)+)
this seems to work fine (allowing for the first 5 characters of string to be leading words).
But when I tell it I only want to start at the beginning of the string with
^([a-zA-Z ]{0,5}(?:\d+\/)+\d+\s*(?:,|and|\s)+)
It only captures On 9/02/2019,.
Strings that begin with the event followed by the date I will need to treat differently
He was walking when he encountered a stray dog on 9/2/19
https://regex101.com/r/p4H10Z/1
Thanks for the rapid responses. I was hoping to figure out how to make it a single match. Now I see. Grouping all of the date matches in another group that can be repeated does the job.
^([a-zA-Z ]{0,5}((?:\d+\/)+\d+\s*(?:,|and|\s)+)+)
I have to cut a string from the fourth stop and I can not seem to find a good way.
Sample String:
"String1","String2","String3","String4","String5"
I can do REGEX pattern like this:
".?",".?",".?",".?,".?(.?)"
and select capture group 1.
But I have several kinds of comma place.
Could someone know a better way to do this without scripts?
Thanks!
How about something like this: (.*?,){4}(.*) The second capture group will have string 5 and beyond.
Do you test your Regex patterns with regex101.com? I find this helps a lot to test patterns.
I am looking to use regex to match a string that has multiple instances of the same text. So for instance in this example:
Some text goes here 357313 More text goes here 654321
Some text goes here 123456 More text goes here 123456
Some text goes here 123456 More text goes here 654321
I would want it to match the second option and not the first and third options. I am fairly new to regex but have spent hours looking online to try and figure out if there is a solution to this problem. The strings are not known in order to use them in the search, I need to use regex to figure out if they match or not.
Any help or assistance would be appreciated!
Thanks!
this matches a line like
[some characters][some digits][some chracters][the same digits as before][some characters]
/.+(\d+).+$1.+/
is that what you are searching for?
edit:
/[^\d]+(\d+)[^\d]+$1[^\d]+/
to make shure the [some characters] are no digits
I believe you want something like the following, where it assumes you want one or more matches of some text followed by your unique string.
/^(.+123456){1,}$/
I just realized you may actually be looking to find strings that contain the same sequence of characters more than once. This doesn't really seem like a problem fit for regex to me. While it may be possible to the more advanced regex users I would say that it may not be a good idea to write such a complicated regex. I would refer you to http://en.wikipedia.org/wiki/Longest_common_substring_problem which may have information that would apply here.
Lets say I have a multiline textbox that I would like to have checked for say, 1-2 digits over a max of 5 lines. I found a regular expression pattern answered on another similar question on here but it was not working for me even after modifying it a number of times.
I'm currently using the following without success.
Dim textCheck As New Regex("(^\d{1,2}$\r?\n?){0,5}", RegexOptions.Multiline)
Could somebody help me out with what I am doing wrong?
Thanks
So you're wanting to match a list of 1 to 2 digit numbers separated by a newline, up to five? if so, this should work. the last newline is optional and if theres anything else in the string it doesn't match. (for this, don't use RegexOptions.Multiline)
I checked this with C#, so I'm not sure if the escape characters are correct. i noticed yours only had 1 slash before the d. in c# you need two, but i removed it from this to make it look like yours.
Dim textCheck As New Regex("^\d{1,2}((\r|\n|\r\n)\d{1,2}){0,4}(\r|\n|\r\n)?$")
First, Grab a copy of RegEx Designer. it's free and worth it's wieght for this kind of thing.
http://www.radsoftware.com.au/?from=RegexDesigner
Then, I think what you might want is something like this
(^\d{1,2}\r?\n?){0,5}\z
and then test that the match includes the entire input. The $ in the middle won't help, the \z forces the match to the end of string. There's probably some details I've missed though. Again, RegExDesigner makes playing with regexes sooooo much more enjoyable!