This question already has answers here:
Regular Expressions: Is there an AND operator?
(14 answers)
Closed 6 years ago.
I am really not good with regular expressions and I come here for some assistance :). I am trying to combine regular expressions with something like AND. For example if we have a text file with:
abc1-xyz
abc1-ertxyz
abc1xyz
postxyz
abc1
I would like to match everything that starts with "abc1" AND also contains the letters "xyz" somewhere.
I know that I can start with:
/^abc1/
but I am not sure how to combine so it can also match to contain "xyz".
Thank you for your assistance in advance.
You should tell us with which language you are coding, regex engines are not always the same.
There is another ambiguous point : Do you need your string to CONTAIN xyz or to END WITH?
Considering you are coding on Javascript..
If you want it to contain xyz, try :
/^abc1.*xyz/
If you want it to end with xyz, try :
/^abc1.*xyz$/
Related
This question already has answers here:
Apply Perl RegExp to Remove Parenthesis and Text at End of String
(1 answer)
Regex for Comma delimited list
(12 answers)
Closed 2 years ago.
I have a bunch of strings such as:
Super Mario Bros. 8 (En,Fr,De,Es,It)
Donald Duck in Whacky Land (En,Fr,De,Es,Sv)
Toadstool Adventures 3D (En)
Chinaland (En,De)
A title which doesn't have any such thing
...
That is, a title of a product followed by (sometimes) a list of one or more language codes in parentheses.
I really struggle to come up with a (PCRE) regexp to safely remove these from the strings in a safe manner. That is, not likely to touch the titles.
I know that ([A-Z]{1}[a-z]{1}) must be involved somewhere, to match a single language code such as "It" or "De", but how I should handle the possibility of any number of such in a row, with commas between or no comma (if it's just one), is beyond my regular expression skills.
I really wish that they had used some kind of unambiguous separator between the title part and the "metadata" part of the filenames... Then I wouldn't need to do all this manual trial-and-error removal. But they didn't.
Something like this would do it:
\([A-Z][a-z](?:,[A-Z][a-z])*\)$
https://regex101.com/r/xxNQ8h/1
Try it like this:
\(([A-Z][a-z],?)+\).*$
Online Demo
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to capture strings containing only one dot. String will mostly contains domain names like
test.com, fun.test.com, lesh.test.com.
I need to check only the first one and to ignore the string that has more than one dots.
How can I do this using regex?
Like this :
^[^.]+\.[^.]+$
Check explanations https://regex101.com/r/mn7Ccr/1
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I have a question about scala regex
The thing I need to do is given a string, I need to find a sub-string up to a specific word given. For example, my regular expression looks like following
val x= "(?s)^(.*)(?=(foo|bar)".r
Then given a string, I need to find the longest sub-string until before foo or bar. This works perfectly but I would like to get the whole string if the string does not contain foo or bar at all.
Right now if I do
x.findAllIn("hello nice to meet you").toArray
it gives me an empty string but I would like to get
"hello nice to meet you" when I do that.
Does anyone have an idea how to implement that?
You can add an end-of-string assertion to the alternative:
(?s)^(.*?)(?=(foo|bar|$))
Demo
This question already has answers here:
Matching numbers with regular expressions — only digits and commas
(10 answers)
regular expression for finding decimal/float numbers?
(9 answers)
Closed 7 years ago.
I need to do a regular expression to match the floats only, what i got is the following :
[\-\+]?[0-9]*(\.[0-9]+)?
But this match also the below
123123132 ,
05/03/1994
I only need want to match the number with the decimal point
Your regex is almost correct for your purpose.
It finds 123123132, because the last part is optional. Removing the ? solves that.
[-+]?[0-9]*(\.[0-9]+)
With that adjustment, it might still find matches in strings like .12/39/3239, if you don't want that to happen, insert enforce matching over the complete string by inserting ^ and $:
^[-+]?[0-9]*(\.[0-9]+)$
How about:
([+-]?[0-9]*\.[0-9]*)
You can see it working here
Here is a regexp handling also existing exponents:
[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?
Debuggex Demo
Additionally you should force the hole string to be matched to avoid matchings within your date values.
^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
By the way here is a nice tutorial about matching floating point numbers using regular expressions: http://www.regular-expressions.info/floatingpoint.html.
This question already has answers here:
Regex to match string containing two names in any order
(9 answers)
Closed 2 years ago.
Given a sentence,
Scheme is such a bizarre programming language.
So any sentence that contains is and language should return true. I found | means or, but couldn't find any symbol means and.
Thanks,
You can use the idiom.
(?=expr)
For example,
(?=.*word1)(?=.*word2)
For more details, please refer to this threads.
Try the following regex:
\bis\b.*\blanguage\b
This one will match if the two words appear in exactly that order. \b (word boundary) means that the words are standalone.
Kinda ugly, but it should work (regardless of the how 'is' and 'language' are ordered):
(.*is.*language.*|.*language.*is.*)
In c# (and I know you didn't ask about c#, but it illustrates how this can be done much quicker)...
string s = "Scheme is such a bizarre programming language.";
if ((s.Contains(" is") || s.Contains("is ")) &&
(s.Contains(" language") || s.Contains("language ")))
{
// found match if you got here
}
Regexs can be slow and hard to parse by someone who is reading your code. Simple string matches are quicker generally.
EDIT: This doesn't care about the order of the words and works for simple whitespace only
Try this one if you don't care about the order of the words in the sentence:
\bis\b.*\blanguage\b|\blanguage\b.*\bis\b