Regex: If the first letter is an X, it should end with either A or B. How do I do this?

Related searches

Regex Problem for Password:

  • Should Start with either X or Y.
  • Followed by an even number (3 digits max).
  • And if the password started with an X, it should end with either an A or a B.

Can anyone help me, please?

Simple RegEx Tutorial, These symbols indicate the start and the end of a string, respectively: You can see that if you don't use either of these two characters, you're saying that the Note that you must always specify the first number of a range (i.e., "{0,2}", not "{,2} "). a string that has lowercase letters 'a' through 'd' (that's equal to "a|b|c|d" and � 1. Line Anchors. In regex, anchors are not used to match characters.Rather they match a position i.e. before, after, or between characters. To match start and end of line, we use following anchors:

1.4. Regular Expressions, [^abc] means "begins with any character but a,b,c", e.g. query [^aeou]ang will match X+ means "X one or more time", e.g. "ba+ng" will match "bang", "baang" but not "bng" You can use this expression if you want to search for annotation starting and ending with \b, at the beginning and/or end of a string, word boundary. ^The matches any string that starts with The -> Try it! end$ matches a matches a string that has either an a or a b or a c The result is the same of the first regex -> Try it

If your regex engine supports it, you can use positive look-ahead like this

(?:X(?=.*[AB]$)|Y)\d{0,2}[02468].*

-- edit

After reading the updated question, this would be a better approach

^(?:X(?=.*([AB]))|Y)\d{0,2}[02468]\1?$

It also uses a back-reference to match the letter captured by the positive look-ahead.

Regular Expression (Regex) Tutorial, One line of regex can easily replace several dozen lines of programming codes. E.g., the regex x matches substring "x" ; regex 9 matches "9" ; regex = matches start-of-word and end-of-word respectively, similar to \b . replaceFirst(), which replaces the first match String replacementStr = "**"; String outputStr = matcher. First, every regex flavor is different, and I didn't want to crowd the page with overly exotic syntax. For a full reference to the particular regex flavors you'll be using, it's always best to go straight to the source.

[PDF] Regular Expressions: The Complete Tutorial, So the regex �1\+1=2� must be written as "1\\+1=2" in C++ code. The C++ or the letter X. Again, the order of the characters and the ranges does not matter. former, however, will match any character that is either not a digit, or is not whitespace. Likewise, �$� will still match at the end of the string (after the last “ e”), and� The anchors are basically used to define that tells the start, the end, or the both, of a line. The ‘^’ symbol can be used to define the pattern which describes what the beginning of the line should be. And the ‘$’ symbol can be used to define the pattern which describes what the end of the line should be. Let’s take a look at this

Regular Expressions :: Eloquent JavaScript, Properly understanding regular expressions will make you a more effective First, since a forward slash ends the pattern, we need to put a backslash The regular expression /\b([01]+b|[\da-f]+h|\d+)\b/ matches either a binary Since there is no x past the end of the string, the star operator tries to match one character less. The first time we encounter the conditional, we have not made a recursive call, so we do not have to match a B. The outer level of the recursive match will be A…C The second time we encounter the conditional, we are in the middle of a recursive call, so we must match a B .

This exactly performs the task specified in the previous paragraph—first matching either the beginning of the string or a hash, then a digit, then either the end of the string or a hash. The desired digits are captured to Group 1.

Comments
  • "Regex" is very vague. There are many different regex engines, with different features and syntaxes. You'll need to figure out what language/library/tool you want to use this regex in, and include that information in the question.
  • (Also, you should tell us what you've already tried, and where you've gotten stuck. This isn't really a "please do my homework for me" site.)
  • @ruakh It was PHP and I managed to do, there's no need to make accessments about someone's question and coming with the "we don't do you homework here" thing. Anyway, I managed to do it with some help from the responsespeople who actually tried to help with the question, cheers.
  • Shouldn't it match a number like 134? Perhaps I am missing something.
  • 134 is an even number. The OP did not say even digits. But that is my interpretation so I could be wrong.
  • @Adrian your answer helped me reach my objective, I saw your response and tweked it so I could enter odd digits too, and managed to do it. And now, seeing how you edited your post, I see the tweaks I made were right, so believe me, you helped a lot :)
  • {1} is useless, (A|B) is better written [AB] and why all these capture groups? They slown down the process.
  • @toto I don't work with regex every day, my friend. I was waiting for jenkins and thought I'd help someone out while I waited. If you've got a better way to do it, you're welcome to post an answer.
  • Op said maximum of 3 digits, so the \\ds should be optional I think: \\d{0,2}
  • Well, I like it! +1
  • Nice! It's compact, but I don't use regular expressions frequently enough to know what's going on w/o docs.
  • @Diadistis Unfortunately, your expression matches on any 3 digit value starting with an even digit and ending with an odd digit. I think it is because the \d{0,2}[02468] is not matched but [02468].* is matched.
  • @WJS I was in the process of updating my answer :)
  • i can certainly relate to that! :)