I have been trying for hours now to figure out this regex and it will not work correctly.
What i need is one that will match the following:
I need it to match IF any word in the entire description is a character followed by a number, without a space
for example:
MC15 this is a test description - MATCH
MC 15 this is another description - NO MATCH
another test MC55 description - MATCH
another test MC 55 description - NO MATCH
i greatly appreciate any help!
thanks for your time!
Can you use "find" instead of "match"? (I.E., the regular expression method you call on your pattern - in order to search for a substring instead of matching against the entire input?) If so, this will do nicely:
([a-zA-Z]+\d+)
Otherwise, it can be expanded to work with "match", using something like:
\b*([a-zA-Z]+\d+)\b*
(?<aMatch>[a-zA-Z]+[0-9]+)
This matches any a-z or A-Z character one or more times followed by any number 0-9 one or more times, no spaces or any other character allowed to separate them
In perl, a regexp like /[a-zA-Z]+\d+/ would work:
[a-zA-Z] denotes any letter
\d denotes any decimal
the + after each character class says it has to be present one or more times
Related
I try to create a regular expression that match the following..
The name starts with a letter and followed by at least 1 numerical value. For example this should be valid: "w1234.pdf" but not this: "ww1234.pdf". So far I only have this:
^[a-zA-Z][0-9]{1}?$
For
The name starts with a letter and followed by at least 1 numerical
value
you can try
^[a-zA-Z][0-9]
pattern. Explanation:
^ starting anchor
[a-zA-Z] single letter
[0-9] followed by a digit
Please, notice that valid names are wider class than provided in the question, e.g a1x456.txt - starting with a letter (a) followed by at least one numerical value (1)
Your regex matches one character in the range [a-zA-Z], followed by one digit. Note that {1}? is useless in this case, since [0-9] already matches one digit.
Change your regex to:
^[a-zA-Z][0-9].+$
if you want to match one digit or more after the first character.
I am having issue with a reg ex expression and can't find the answer to my question.
I am trying to build a reg ex pattern that will pull in any matches that have # around them. for example #match# or #mt# would both come back.
This works fine for that. #.*?#
However I don't want matches on ## to show up. Basically if there is nothing between the pound signs don't match.
Hope this makes sense.
Thanks.
Please use + to match 1 or more symbols:
#+.+#+
UPDATE:
If you want to only match substrings that are enclosed with single hash symbols, use:
(?<!#)#(?!#)[^#]+#(?!#)
See regex demo
Explanation:
(?<!#)#(?!#) - a # symbol that is not preceded with a # (due to the negative lookbehind (?<!#)) and not followed by a # (due to the negative lookahead (?!#))
[^#]+ - one or more symbols other than # (due to the negated character class [^#])
#(?!#) - a # symbol not followed with another # symbol.
Instead of using * to match between zero and unlimited characters, replace it with +, which will only match if there is at least one character between the #'s. The edited regex should look like this: #.+?#. Hope this helps!
Edit
Sorry for the incorrect regex, I had not expected multiple hash signs. This should work for your sentence: #+.+?#+
Edit 2
I am pretty sure I got it. Try this: (?<!#)#[^#].*?#. It might not work as expected with triple hashes though.
Try:
[^#]?#.+#[^#]?
The [^ character_group] construction matches any single character not included in the character group. Using the ? after it will let you match at the beginning/end of a string (since it matches the preceeding character zero or more times. Check out the documentation here
Does anyone know how to say I can get a regex (C#) search of the first 3 letters of a full name?
Without the use of (.*)
I used (.**)but it scrolls the text far beyond the requested name, or
if it finds the first condition and after 100 words find the second condition he return a text that is not the look, so I have to limit in number of words.
Example: \s*(?:\s+\S+){0,2}\s*
I would like to ignore names with less than 3 characters if they exist in name.
Search any name that contains the first 3 characters that start with:
'Mar Jac Rey' (regex that performs search)
Should match:
Marck Jacobs L. S. Reynolds
Marcus Jacobine Reys
Maroon Jacqueline by Reyils
Can anyone help me?
The zero or more quantifier (*) is 'greedy' by default—that is, it will consume as many characters as possible in order to finding the remainder of the pattern. This is why Mar.*Jac will match the first Mar in the input and the last Jac and everything in between.
One potential solution is just to make your pattern 'non-greedy' (*?). This will make it consume as few characters as possible in order to match the remainder of the pattern.
Mar.*?Jac.*?Rey
However, this is not a great solution because it would still match the various name parts regardless of what other text appears in between—e.g. Marcus Jacobine Should Not Match Reys would be a valid match.
To allow only whitespace or at most 2 consecutive non-whitespace characters to appear between each name part, you'd have to get more fancy:
\bMar\w*(\s+\S{0,2})*\s+Jac\w*(\s+\S{0,2})*\s+Rey\w*
The pattern (\s+\S{0,2})*\s+ will match any number of non-whitespace characters containing at most two characters, each surrounded by whitespace. The \w* after each name part ensures that the entire name is included in that part of the match (you might want to use \S* instead here, but that's not entirely clear from your question). And I threw in a word boundary (\b) at the beginning to ensure that the match does not start in the middle of a 'word' (e.g. OMar would not match).
I think what you want is this regular expression to check if it is true and is case insensitive
#"^[Mar|Jac|Rey]{3}"
Less specific:
#"^[\w]{3}"
If you want to capture the first three letters of every words of at least three characters words you could use something like :
((?<name>[\w]{3})\w+)+
And enable ExplicitCapture when initializing your Regex.
It will return you a serie of Match named "name", each one of them is a result.
Code sample :
Regex regex = new Regex(#"((?<name>[\w]{3})\w+)+", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
var match = regex.Matches("Marck Jacobs L. S. Reynolds");
If you want capture also 3 characters words, you can replace the last "\w" by a space. In this case think to handle the last word of the phrase.
I'm currently having difficulty matching strings with my regex. The objective is to match:
One or two letters
One, two or three numbers
Zero or one asterisk
Such as U21, F305 and H12*. The regex that I'm using is:
\D{1,2}\d{1,3}\*?
However, it's been matching strings like:
3.0L
6HBW20
3/8"
Y1015
I'm not too bright with regex, but this is holding me from completing my project. Can anyone help me out?
Thank you.
Try using /^[a-zA-Z]{1,2}\d{1,3}\*?$/
The anchors ^ and $ are useful to make sure that you match exactly the pattern you intend. Read up on them :)
You need to anchor your match. ^ anchors the match to start of line; $ drops anchor at end of line.
Try this regular expression
#"^[\p{L}]{1,2}\d{1,3}[*]?$"
\D matches any non-digit, which is a much larger set than just letters (basically everything else, including periods, slashes, etc). Try using [a-zA-Z]{1,2} to match 1 or 2 letters.
[a-zA-Z]{1,2}\d{1,3}\*?
I wrote a very simple regular expression that need to match the next pattern:
word.otherWord
- Word must have at least 2 characters and must not start with digit.
I wrote the next expression:
[a-zA-Z][a-zA-Z](.[a-zA-Z0-9])+
I tested it using Regex tester and it seems to be working at most of the cases but when I try some inputs that ends with 'e' it's not working.
for example:
Hardware.Make does not work but Hardware.Makee is works fine, why? How can I fix it?
That's because your regex looks for inputs which length is even.
You have two characters matched by [a-zA-Z][a-zA-Z] and then another two characters matched by (.[a-zA-Z0-9]) as a group which is repeated one or more times (because of +).
You can see it here: http://regex101.com/r/fW2bC1
I think you need that:
[a-zA-Z]+(\.[a-zA-Z0-9]+)+
Actually, the dot is a regex metacharacter, which stands for "any character". You'll need to escape the dot.
For your situation, I'd do this:
[a-zA-Z]{2,}\.[a-zA-Z0-9]+
The {2,} means, at least 2 characters from the previous range.
In regex, the dot period is one of the most commonly used metacharacters and unfortunately also commonly misused metacharacter. The dot matches a single character without caring what that character is...
So u would also re-write it like
[a-zA-Z]+(\.[a-zA-Z0-9]+)+