I am new to regex, basically I'd like to check if a word has ONLY one colons or not.
If has two or more colons, it will return nothing.
if has one colon, then return as it is. (colon must be in the middle of string, not end or beginning.
(1)
a:bc:de #return nothing or error.
a:bc #return a:bc
a.b_c-12/:a.b_c-12/ #return a.b_c-12/:a.b_c-12/
(2)
My thinking is, but this is seems too complicated.
^[^:]*(\:[^:]*){1}$
^[-\w.\/]*:[-\w\/.]* #this will not throw error when there are 2 colons.
Any directions would be helpful, thank you!
This will find such "words" within a larger sentence:
(?<= |^)[^ :]+:[^ :]+(?= |$)
See live demo.
If you just want to test the whole input:
^[^ :]+:[^ :]+$
To restrict to only alphanumeric, underscore, dashes, dots, and slashes:
^[\w./-]+:[\w./-]+$
I saw this as a good opportunity to brush up on my regex skills - so might not be optimal but it is shorter than your last solution.
This is the regex pattern: /^[^:]*:[^:]*$/gm and these are the strings I am testing against: 'oneco:on' (match) and 'one:co:on', 'oneco:on:', ':oneco:on' (these should all not match)
To explain what is going on, the ^ matches the beginning of the string, the $ matches the end of the string.
The [^:] bit says that any character that is not a colon will be matched.
In summary, ^[^:] means that the first character of the string can be anything except for a colon, *: means that any number of characters can come after and be followed by a single colon. Lastly, [^:]*$ means that any number (*) of characters can follow the colon as long as they are not a colon.
To elaborate, it is because we specify the pattern to look for at the beginning and end of the string, surrounding the single colon we are looking for that only the first string 'oneco:on' is a match.
Related
I'm using Regex to match whole sentences in a text containing a certain string. This is working fine as long as the sentence ends with any kind of punctuation. It does not work however when the sentence is at the end of the text without any punctuation.
This is my current expression:
[^.?!]*(?<=[.?\s!])string(?=[\s.?!])[^.?!]*[.?!]
Works for:
This is a sentence with string. More text.
Does not work for:
More text. This is a sentence with string
Is there any way to make this word as intended? I can't find any character class for "end of text".
End of text is matched by the anchor $, not a character class.
You have two separate issues you need to address: (1) the sentence ending directly after string, and (2) the sentence ending sometime after string but with no end-of-sentence punctuation.
To do this, you need to make the match after string optional, but anchor that match to the end of the string. This also means that, after you recognize an (optional) end-of-sentence punctuation mark, you need to match everything that follows, so the end-of-string anchor will match.
My changes: Take everything after string in your original regex and surround it in (?:...)? - the (?:...) being a "non-remembered" group, and the ? making the entire group optional. Follow that with $ to anchor the end of the string.
Within that optional group, you also need to make the end-of-sentence itself optional, by replacing the simple [.?!] with (?:[.?!].*)? - again, the (?:...) is to make a "non-remembered" group, the ? makes the group optional - and the .* allows this to match as much as you want after the end-of-sentence has been found.
[^.?!]*(?<=[.?\s!])string(?:(?=[\s.?!])[^.?!]*(?:[.?!].*)?)?$
The symbol for end-of-text is $ (and, the symbol for beginning-of-text, if you ever need it, is ^).
You probably won't get what you're looking for with by just adding the $ to your punctuation list though (e.g., [.?!$]); you'll find it works better as an alternative choice: ([.?!]|$).
Your regex is way too complex for what you want to achieve.
To match only a word just use
"\bstring\b"
It will match start, end and any non-alphanum delimiters.
It works with the following:
string is at the start
this is the end string
this is a string.
stringing won't match (you don't want a match here)
You should add the language in the question for more information about using.
Here is my example using javascript:
var reg = /^([\w\s\.]*)string([\w\s\.]*)$/;
console.log(reg.test('This is a sentence with string. More text.'));
console.log(reg.test('More text. This is a sentence with string'));
console.log(reg.test('string'))
Note:
* : Match zero or more times.
? : Match zero or one time.
+ : Match one or more times.
You can change * with ? or + if you want more definition.
I know that there are a lot of topics like this one. I've spent a lot of hours checking expressions to make my code work. I don't really understand how regex work, so I hope you can help me out.
I want to validate this inputs (I hope I am not pushing it)
Only letters (with latin characters too)
Address (including dots, commas, colon, number sign and hyphen)
Telephone (numbers and hyphen)
like:
/[a-zA-ZÑñÁáÉéÍíÓóÚú]+$/ /* Only letters */
/[a-zA-Z0-9\sñáéíóúü .,:#-]+$/ /* Address */
/^[\d-]+$/ /* Telephone */
They work fine, when I include an special character at the end of the string but if I enter that special character between accepted characters it does not work. Allow me to write an example please:
For the "Only letters" expression:
ab[(% - Does not pass
a[(%b - It pass and it shouldn't!
Thanks a lot for your time, any help will be appreciate!
You forgot the ^ start of string anchor at the beginning of the 2 first patterns.
See demo 1:
^[a-zA-ZÑñÁáÉéÍíÓóÚú]+$
^
Same with the second regex. There, you also have a literal space and \s, so literal space can be removed:
^[a-zA-Z0-9\sñáéíóúü.,:#-]+$
^
See demo 2
And as for your third regex, it is not optimal since it will match ----1123.
Use
/^(?:\d+-)+\d+$/
See demo 3. Here, we match sequences of digits and hyphen (with (?:\d+-)+) and then a sequence of digits, from beginning till end.
The expression /[..]+$/ says that the test subject must have any of the characters (..) at its end. $ symbolises the end of the string. The beginning of the string does not have to match. If you want to enforce that for the entire string, use the beginning anchor as well:
/^[..]+$/
This now says the string must have any of the characters (..) between its beginning and end, and there's no room for anything else.
You're already doing this for the telephone regex.
I am trying to validate a sentence. It starts with alphabets, contains numbers and special characters like '-,() and may end with : or . I am trying to find an expression that can match the following pattern.
I'm trying to-achieve such(this), kind of pattern:
I have tried using ^[a-zA-Z]+([ '/-]{0,1}+([()]{0,1}[,]{0,1})+[a-zA-Z0-9.]+[:]??)+$ , but am facing a problem at getting ',' after a closing ')' followed by space.
Can someone please help me.
Thanks
Let's make sure I understand what you're going for:
Your regex will match an entire sentence, meaning any string that starts with a letter of the alphabet and ends with a colon or period.
This sentence may contain numbers and special characters; really any character except for a colon or period, which would signal the end of the sentence.
If so, then all you need is this:
^[A-Za-z][^\.:]*[\.:]$
^ matches the beginning of the string.
[A-Za-z] matches any letter of the alphabet, upper- or lower-case.
[^\.:]* matches 0 or more characters of any kind as long as they are not a colon or a period.
[\.:] matches a colon or a period.
$ matches the end of the string.
This will only work if the string you're matching is the sentence and nothing else. To match a sentence that is part of a larger string, try removing the ^ at the beginning and the $ at the end, and using the /g (multiple matches) tag if it meets your needs.
I am new to perl language - I have been trying to understand the below code
if ( $nextvalue !~ /^.+"[^ ]+ \/cs\/.+\sHTTP\/[1-9]\.[0-9]"|\/\/|\/Images\/fold\/1.jpg|\/busines|\/Type= OPTIONS|\/203.176.111.126/)
Can you please help us understand what is above meant for?
condition will be true when $nextvalue will NOT match following regular expression.
Regular expressiion will match if that string
either
starts with at least one character,
followed by double quote sign ("),
followed by at least one non-whitespace character,
followed by whitespace (),
followed by string "/cs/",
followed by at least one character,
followed by whitespace and string HTTP/,
followed by one of digits from 1 to 9 inclusive,
followed by dot
followed by one of digits from 0 to 9,
followed by double quote mark (")
or contains two forward slashes (//)
or contains sunstring "/Images/fold/1.jpg"
or contains substring "/busines"
or contains substring "/Type= OPTIONS"
or contains substring "/203.176.111.126"
Whenever i am unsure what some cryptic regular expression does, i turn to Debuggex:
^.+"[^ ]+ \/cs\/.+\sHTTP\/[1-9]\.[0-9]"|\/\/|\/Images\/fold\/1.jpg|\/busines|\/Type= OPTIONS|\/203.176.111.126
Debuggex Demo
This is a railroad diagram, every string that has a substring fitting the description along any of the grey tracks will match your regex. As your condition uses !~ meaning "does not match", those strings will then fail the check.
Debuggex certainly has issues (for example it displays ^, meaning you would have to know that this means the beginning of the string, same for dots and other, whitespaces show up as underscroes, etc.) but it certainly helps in understanding the structure of the expression and possibly gives you an idea what the author had in mind.
I am looking for a regex that matches first word in a sentence excluding punctuation and white space. For example: "This" in "This is a sentence." and "First" in "First, I would like to say \"Hello!\""
This doesn't work:
"""([A-Z].*?(?=^[A-Za-z]))""".r
(?:^|(?:[.!?]\s))(\w+)
Will match the first word in every sentence.
http://rubular.com/r/rJtPbvUEwx
This is an old thread but people might need this like I did.
None of the above works if your sentence starts with one or more spaces.
I did this to get the first (non empty) word in the sentence :
(?<=^[\s"']*)(\w+)
Explanation:
(?<=^[\s"']*) positive lookbehind in order to look for the start of the string, followed by zero or more spaces or punctuation characters (you can add more between the brackets), but do not include it in the match.
(\w+) the actual match of the word, which will be returned
The following words in the sentence are not matched as they do not satisfy the lookbehind.
You can use this regex: ^[^\s]+ or ^[^ ]+.
You can use this regex: ^\s*([a-zA-Z0-9]+).
The first word can be found at a captured group.
[a-z]+
This should be enough as it will get the first a-z characters (assuming case-insensitive).
In case it doesn't work, you could try [a-z]+\b, or even ^[a-z]\b, but the last one assumes that the string starts with the word.