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.
Related
I've looked everywhere and haven't been able to find a question that answers this specific use case (maybe I've missed it). But basically I'm wanting to extract the following text from a string: Welcome James:
This text must be at the start of the string, e.g:
Welcome James: Now some text follows...blahblah - This would be a match
However
This is some text Welcome James: some more text... - This would not be a match.
So basically I'd hard code Welcome James: into the regex (I don't need any other variables of Welcome <name>:.
Is this possible? All I've been able to find is regexes that match single words without spaces or characters.
To search at the start of a string, just prefix the regex with the ^ (caret) character:
/^Welcome James/
Here is the answer :) But #charles gave it too!
^(Welcome James)
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.
I have a bunch of lines with IDs as the first six characters, and data I don't need after. Is there a way to identify everything after the ID section so Find and Replace can replace it with whitespace?
/.{6}\K.*//
If you want something more specific, please be more specific in your question.
I would like to find something like this:
-(IBOutlet)UIView *aView;
I would like to find aView, something that I can confirm is -(IBOutlet) must be a prefix, but it comes with not ensure a space or another string, after that, we need to string that must begin with '*', until it match the ;.
So, my regex look like that:
(IBOutlet)*\*?;
For sure, it can't capture what I want. Any advise?
You just have to build it up incrementally. The best reference that I have found (by far) is http://www.regular-expressions.info. After learning the basics, you can then use one of many online pattern matching tools, here is one:
https://regex101.com
With that, your goal is easily determined (with some allowances for free space):
^\s*-\s*\(IBOutlet\)(\w*)\s*(\*\w*)
First problem: you don't have a capturing group so how do you get aView back after the match?
Second, the \*? means "match the * character literally, 0 or 1 times", which I guess isn't what you want either.
Try this pattern:
(IBOutlet)*\*(.+);
RegEx 101 can explain what each component means.
So I wanted to limit a textbox which contains an apartment number which is optional.
Here is the regex in question:
([0-9]{1,4}[A-Z]?)|([A-Z])|(^$)
Simple enough eh?
I'm using these tools to test my regex:
Regex Analyzer
Regex Validator
Here are the expected results:
Valid
"1234A"
"Z"
"(Empty string)"
Invalid
"A1234"
"fhfdsahds527523832dvhsfdg"
Obviously if I'm here, the invalid ones are accepted by the regex. The goal of this regex is accept either 1 to 4 numbers with an optional letter, or a single letter or an empty string.
I just can't seem to figure out what's not working, I mean it is a simple enough regex we have here. I'm probably missing something as I'm not very good with regexes, but this syntax seems ok to my eyes. Hopefully someone here can point to my error.
Thanks for all help, it is greatly appreciated.
You need to use the ^ and $ anchors for your first two options as well. Also you can include the second option into the first one (which immediately matches the third variant as well):
^[0-9]{0,4}[A-Z]?$
Without the anchors your regular expression matches because it will just pick a single letter from anywhere within your string.
Depending on the language, you can also use a negative look ahead.
^[0-9]{0,4}[A-Za-z](?!.*[0-9])
Breakdown:
^[0-9]{0,4} = This look for any number 0 through 4 times at the beginning of the string
[A-Za-z] = This look for any characters (Both cases)
(?!.*[0-9]) = This will only allow the letters if there are no numbers anywhere after the letter.
I haven't quite figured out how to validate against a null character, but that might be easier done using tools from whatever language you are using. Something along this logic:
if String Doesn't equal $null Then check the Rexex
Something along those lines, just adjusted for however you would do it in your language.
I used RegEx Skinner to validate the answers.
Edit: Fixed error from comments