I want a regix format that Must be alphabets and special characters (like space, ‘, -) but numeric value should not be taken.
I tried with this expression /^[a-zA-Z ]*$/ but it treats space as special character.
Please Help.
/^[a-zA-Z\s\-\'\"]*$/
use this.
This will contain any alphabet([upper/lower]case)
,space,
hiphen,
",
'
update
If you are using it inside NSPredicate
then make sure that you put the - in the end, as it throws error.
Move it to the end of the sequence to be the last character before the closing square bracket ].
like this [a-zA-Z '"-]
If you want only the alphabets and space, ' and - then:
/^[-a-zA-Z\s\']+$/
Notice the + from above instead of *. If you use * then it will match with empty string, where the + sign means to have at least one character in your input.
Now, if you want to match any alphabets with any special characters(not only those three which are mentioned), then I'll just you to use this one:
/^\D+$/
It means any characters other than digits!
Maybe try this:
\b[a-zA-Z \-\']+\b
http://regex101.com/r/oQ5nU9
You can use it defiantly work it
[a-zA-Z._^%$#!~#,-]+
this code work fine you can try it....
//Use this for allowing space as we all as other special character.
#"[a-zA-Z\\s\\-\\'\\"]"
//Following link will be help for further.
http://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet
Thanks for your response.. I finally resolved it with this
NSString characterRegex = #"^(\s[a-zA-Z]+(([\'\-\+\s]\s*[a-zA-Z])?[a-zA-Z])\s)+$";
NSPredicate *characterTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#",characterRegex];
return [characterTest evaluateWithObject:inputString];
Related
I want to split a text into it's single words using regular expressions. The obvious solution would be to use the regex \\b unfortunately this one does split words also on the hyphen.
So I am searching an expression doing exactly the same as the \\b but does not split on hyphens.
Thanks for your help.
Example:
String s = "This is my text! It uses some odd words like user-generated and need therefore a special regex.";
String [] b = s.split("\\b+");
for (int i = 0; i < b.length; i++){
System.out.println(b[i]);
}
Output:
This
is
my
text
!
It
uses
some
odd
words
like
user
-
generated
and
need
therefore
a
special
regex
.
Expected output:
...
like
user-generated
and
....
#Matmarbon solution is already quite close, but not 100% fitting it gives me
...
like
user-
generated
and
....
This should do the trick, even if lookaheads are not available:
[^\w\-]+
Also not you but somebody who needs this for another purpose (i.e. inserting something) this is more of an equivalent to the \b-solutions:
([^\w\-]|$|^)+
because:
There are three different positions that qualify as word boundaries:
Before the first character in the string, if the first character is a word character.
After the last character in the string, if the last character is a word character.
Between two characters in the string, where one is a word character and the other is not a word character.
--- http://www.regular-expressions.info/wordboundaries.html
You can use this:
(?<!-)\\b(?!-)
I need to convert input string which is actual array of regexps separated by some delimiter.
Output - is list of strings where each string is regexp from input.
Question is what delimiter should I use to be sure that I will receive correct values.
Because it seems like regexp string could contain any set of characters, and in this case I need to decide what should be better for use as delimiter.
Thanks.
Building on #Theox's answer, a triple + is not valid in a regular expression and, assuming you expect the values to be valid regular expressions, could be used as a delimiter.
regex1+++regex2+++regex3
If a regular expression ended with a + or a double +, you'd have 4 or 5 + characters in a row. But, since a regular expression cannot start with a +, you'd know that the last three + characters represent the delimiter. For example,
a+++++b
would represent two regular expressions: a++ and b.
Note that the double + is valid in a regular expression with the second + being the possessive quantifier so we cannot use only two + characters as the delimiter.
You say its an Input-String and I assume you are able to manipulate it.
Why don't you use doubled character as delimiter? For example, I don't think you will use double semicolon in your regex, or triple.
regex1;;regex2;;regex3
Then
regexString.split(";;", regexString);
I think you could use a double + as your delimiter.
It seems impossible to have a double + in a regexp, as it is a quantifier and it must be escaped to match the character.
So regexp1++regexp2++regexp3 will work fine.
Edit : After seeing Rangi Keen's comment : two + is not enough, as it is still valid, but three + (or more) should do it !
regexp1+++regexp2+++regexp3 will answer your problem.
i have following statement {$("#aprilfoolc").val("HoliWed27"); $("#UgadHieXampp").val("ugadicome");}.and i want to get the string with combination.i have written following regex but it is not working.
please help!
(?=[\$("#]?)[\w]*(?<=[")]?)
Your lookaround assertions are using character classes by mistake, and you've confused lookbehind and lookahead. Try the following:
(?<=\$\(")\w*(?="\))
You could use this simpler one :
'{$("#aprilfoolc").val("HoliWed27");}'.match(/\$\(\"#(\w+)\"[^"]*"(\w+)"/)
This returns
["$("#aprilfoolc").val("HoliWed27"", "aprilfoolc", "HoliWed27"]
where the strings you want are at indexes 1 and 2.
This construction
(?=[\$*"#]?)
will match a lookahead, but only optional -- the character set is followed by a ?. This kind of defeats the next part,
[\w]
which matches word characters only. So the lookahead will never match. Similar, this part
(?<=[")])
will also never match, because logically there can never be one of the characters " or ) at the end of a string that matches \w only. Again, since this portion is optional (that ? at the end again) it will simply never match.
It's a bit unclear what you are after. Strings inside double quotes, yes, but in the first one you want to skip the hash -- why? Given your input and desired output, this ought to work:
\w+(?=")
Also possible:
/\("[#]?(.*?)"\)/
import re
s='{$("#aprilfoolc").val("HoliWed27");}'
f = re.findall(r'\("[#]?(.*?)"\)',s)
for m in f:
print m
I don't know why, but if you want capturing of two groups simultaneously, so:
/\("#(.*?)"\).*?\("(.*?)"\)/
import re
s='{$("#aprilfoolc").val("HoliWed27");}'
f = re.findall(r'\("#(.*?)"\).*?\("(.*?)"\)',s)
for m in f:
print m[0],m[1]
In JavaScript:
var s='{$("#aprilfoolc").val("HoliWed27")';
var re=/\("#(.*?)"\).*?\("(.*?)"\)/;
alert(s.match(re));
I am trying to validate a comma separated list for numbers 1-8.
i.e. 2,4,6,8,1 is valid input.
I tried [0-8,]* but it seems to accept 1234 as valid. It is not requiring a comma and it is letting me type in a number larger than 8. I am not sure why.
[0-8,]* will match zero or more consecutive instances of 0 through 8 or ,, anywhere in your string. You want something more like this:
^[1-8](,[1-8])*$
^ matches the start of the string, and $ matches the end, ensuring that you're examining the entire string. It will match a single digit, plus zero or more instances of a comma followed by a digit after it.
/^\d+(,\d+)*$/
for at least one digit, otherwise you will accept 1,,,,,4
[0-9]+(,[0-9]+)+
This works better for me for comma separated numbers in general, like: 1,234,933
You can try with this Regex:
^[1-8](,[1-8])+$
If you are using python and looking to find out all possible matching strings like
XX,XX,XXX or X,XX,XXX
or 12,000, 1,20,000 using regex
string = "I spent 1,20,000 on new project "
re.findall(r'(\b[1-8]*(,[0-9]*[0-9])+\b)', string, re.IGNORECASE)
Result will be ---> [('1,20,000', ',000')]
You need a number + comma combination that can repeat:
^[1-8](,[1-8])*$
If you don't want remembering parentheses add ?: to the parens, like so:
^[1-8](?:,[1-8])*$
Is is it possible to remove all characters before (and including) every character to third ' and also everything after (and including) the fourth ', basically isolating the text inside the 3rd and 4th '
example:
a, 'something', 'ineedthistext', 'moretexthere'
should result in
ineedthistext
Regex might not be the best tool to do this (split by comma/apostrophe might actually be a better way), but if you want regex...
Maybe instead of removing all the characters before and after ineedthistext, you can capture ineedthistext from the group.
I would use something like:
^.*?'.*?'.*?'(.*?)'
Tested with rubular.
Try
public String stringSplit(String input) {
String[] wordArray = input.split("'");
String requiredText = wordArray[3];
return requiredText;
}
This will work if you always want the bit between the 3rd and 4th '.
Derived from this answer, a possible solution is:
Regex.Match(yourString, #"\('[^']*)\)").Groups[2].Value
The code looks for all strings embedded between 2 single quotes, and puts them in groups. You need the 2nd group.
To alter your string directly, effectively removing the unwanted characters, you could use:
yourString = Regex.Match(yourString, #"\('[^']*)\)").Groups[2].Value