I need regular expression (C#) for symbol * - it should match any number of any characters, but it can contain only one space. I tried following, but its not working:
#".*[^[\t\0x0020]^[\t\0x0020]+].*"
#".*[^\s^\s+].*"
#".*[^\s\s+].*"
any way how to create regex like this?
Example: If user write expression MTN*-* it has to match for example
MTN3111-0000
but not
MTN311100 MTN3111-0000
You could use this expression:
\S*\s?\S*
It would match any number of any characters, but allow at most one space.
Here:
#"[^\s]*\s?[^\s]*"
Then there may be some specifics depending on other requirements
here try this:
([^\-]+\-[^\s]+)\s
Related
i just want to match this string with regex:
How can i do this?
/profil
i try do this in this way:
.*/profil.*
But my software dont match any results in text.
All you need is this
#"(\/profil)"
if all you need is to match "/profil" then there is no need for the ".*"
Remember the expressions are greedy.
The . matches any character, and * tells it that this . can go on forever. So this expression will eat up all of your input, leaving nothing for the /profil part.
It seems like you're trying to put a wildcard around /profil. This is not needed with regular expressions. You should just be able to use /profil as the full expression and match your string.
I am extracting all numbers used in an xml file. The numbers are written in following two patterns
<Environment Id="11" StringId="8407" DescriptionId="5014" RemoteControlAppStringId="8119; 8118" EnvironmentType="BlueToothBridge" AlternateId="1" XML_NAME_ID="BTBSpeechPlusM" FactoryGainType="LIN18">
<Offsets />
</Environment>
I am using regex: "\"\d*;\"" and "\"\d*\"" to extract all numbers.
from the above when i ran Regex "\"\d*\"" using
Regex.Match(myString, "\"\\d*\"")
the above line returns 8407, 11,5014 but it is not returning 8119 and 8118
Your regex will fail to match 8119; 8118 because your pattern is finding quoted numbers.
try with
\b\d+\b
\b specify that \d+ will match only in word boundary. So LIN18 will not match.
Depening on whether you can assume that the provided input is valid XML, you could use the following regular expression:1
Regex.match(myString, "(?<=\")\\d+(?=\")|(?<=\")\\d+(?=; ?\\d+\")|(?<=\"\\d+; ?)\\d+(?=\")" )
The main idea behind this is that it takes the three possible situations into account:
"[number]"
"[number]; [other_number]" (With or without a space before [other_number])
"[other_number]; [number]" (With or without a space before [number])
There are two new concepts I included in the regular expression:2
Positive lookahead: (?=[regex])
Positive lookbehind: (?<=[regex])
These concepts allow the regular expression to check if something specific is before or after it, without putting it in the match.
This regular expression could easily be optimised, but this is meant as an example of a basic approach.
One good tip for developing a regular expression like this is to use a tool (online or offline) to test your regular expression. The tool I used was .NET Regex Tester.
As #poke stated in the comment, it's because your regex doesn't match the string. Change your regex to capture specific matches and account for the possibility of the ';'.
Something like below should probably do the trick.
EDIT: (\b\d+\b)|(\b\d+[;*]\d+\b)
I'm horrible at regex so please bear with me here:
I need to a match where the first character can be anything and the next two have to be RS.
so...
XRS123445 - Match
I suggest you start reading this. Matching any character at a position is basically the simplest thing you can do with regular expressions. There are many different things you can use too:
Any alphanumeric character(\w)
Any character whatsoever(.)
A range of characters ([A-Z])
Any character in a certain unicode range ([\uxxx-\uxxx])
and more. You should also be careful as certain regex languages have ceratin nuances and certain flags have to be set to get the same result. I wouldn't get into more detail to avoid confusion here.
This is the regex you're looking for:
^.RS.*
This would match on any of these:
XRS123445
4RSabc
YRS
.RS.*
Should match as . means any character and then RS as per your requirements
Use this pattern
var pattern = "^.RS";
I want a regular expression that accepts all numbers, alphabets and only the hyphen (‐) from special characters.
I am trying this expression: ^\d+$/[-]/[a-z] but it does not work. I want to accept expressions like this one:
Emp-IN-0000001
Can someone help me with this?
If it's always this format (Emp-IN-0000001), then use this regexp:
^[a-zA-Z]+-[a-zA-Z][a-zA-Z]-[0-9]+$
or, if you have extended regexps:
^[a-zA-Z]+-[a-zA-Z]{2}-\d+$
when there are always seven digits, use this:
^[a-zA-Z]+-[a-zA-Z]{2}-\d{7}$
You can even say:
^Emp-IN-\d{7}$
if it's exactly "Emp-IN-" + digits.
Btw, this is not C# specific, you can use these regular expressions with any language, as long as they support regexps at all.
If you stickily wants to follow this format Emp-IN-0000001, then you might need to use this regular expression:
^[a-zA-Z]+-[a-zA-Z]+-\d+$
I don't really get what you tried with your regular expression, but it is actually as simple as this:
^[a-zA-Z\d-]+$
Or if you want to allow empty strings:
^[a-zA-Z\d-]*$
If you use the case-insensitive modifier with your regular expression, you can leave out either the a-z or A-Z from both variants.
I recommend you read up on some regex basics in this great tutorial.
I have code that searches a folder that contains SQL patch files. I want to add file names to an array if they match the following name format:
number-text-text.sql
What Regex would I use to match number-text-text.sql?
Would it be possible to make the Regex match file names where the number part of the file name is between two numbers? If so what would be the Regex syntax for this?
The following regex make it halfway there:
\d+-[a-zA-Z]+-[a-zA-Z]+\.sql
Regarding to match in a specific range it gets trickier as regex doesn't have a simple way to handle ranges. To limit the match to a filename with a number between 2 and 13 this is the regex:
([2-9]|1[0-3])-[a-zA-Z]+-[a-zA-Z]+\.sql
Your regular expression should be:
(\d+)-[a-zA-Z]+-[a-zA-Z]+\.sql
You would then use the first captured group to check if your number is between the two numbers you desire. Don't try to check if a number is within a range with a regular expression; do it in two steps. Your code will be much clearer for it.
How about:
\d+-[^-]+-[^-]+\.sql
Edit: You want just letters, so here it is without specific ranges.
\d+-[a-z]+-[a-z]+\.sql - You'll also want to use the i flag, not sure how that's done in c#, but here it is in js/perl:
/\d+-[a-z]+-[a-z]+\.sql/i
Ranges are more difficult. Here's an example of how to match 0-255:
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
So to match (0-255)-text-text.sql, you'd have this:
/^(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])-[a-z]+-[a-z]+\.sql/i
(I put the digits in a non-capturing group and matched from the beginning of the string to prevent partial matches on the number and in case you're expecting numbered groups or something).
Basically every time you need another digit of possibility, you'll need to add a new condition inside this case. The smaller the digit you'd like to match, the more cases you'll need as well. What is your desired min/max? AFAIK there's not a simple way to do this dynamically (although I'd love for someone to show me I'm wrong about that).
The simplest way to get around this would be to simply capture the digits, and use native syntax to see if it's in your range. Example in js:
var match = filename.match(/(\d+)-[a-z]+-[a-z]+\.sql/i);
if(match && match[1] < maximumNumber && match[1] > minimumNumber){
doStuff();
}
This should work:
select '4-dfsg-asdfg.sql' ~ E'^[0-9]+-[a-zA-Z]+-[a-zA-Z]+\\.sql$'
This restricts the TEXT to simple ASCII characters. May or may not be what you want.
This is tested in PostgreSQL. Regular expression flavors differ a lot between implementations. You probably know that?
Anchors at begin ^ and end $ are optional, depending how you are going to do it.