problem in not replaceing minus sign(-) with a blank using regex - regex

I am using this regex expression to replace some characters with ""
I used it as
query=query.replace(/[^a-zA-Z 0-9 * ? : . + - ^ "" _]+/g,'');
But when my query is as +White+Diamond, i get result +White+Diamond, but when query is -White+diamond i am getting White+diamond, it means - is replaced by "" that i don't want.
Please tell me what is the problem.

In regex, - means "from ... to ...", escape your - with a backslash: \-.

What SteeveDroz said:
query=query.replace(/[^a-zA-Z0-9*?:.+\-^"_ ]+/g,'');
I'm assuming you want to exclude spaces as well. If not, remove the final space from the character class.

Related

Regex to evaluate phone number in Pentaho [duplicate]

I wanted to remove the special characters like ! # # $ % ^ * _ = + | \ } { [ ] : ; < > ? / in a string field.
I used the "Replace in String" step and enabled the use RegEx. However, I do not know the right syntax that I will put in "Search" to remove all these characters from the string. If I only put one character in the "Search" it was removed from the string. How can I remove all of these??
This is the picture of how I did it:
As per documentation, the regex flavor is Java. You may use
\p{Punct}
See the Java regex syntax reference:
\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?#[]^_`{|}~

regexp for renaming multiple files using 'rename.pl'

I'm using rename.pl to rename a multiple files. I am having trouble coming up with the right regexp. My file names are of the form:
nn.some.title.string.ext
I want to change just the first '.' to ' - '. I thought this would work but it does not.
s/\.?/ - /
Can someone help me out with this? TIA.
\.? can match a sequence of zero characters, so s/\.?/ - / replaces the dot or the empty string at the start of the input.
"abc.def.ghi" ⇒ " - abc.def.ghi"
".abc" ⇒ " - abc"
To replace the first ., you can use the following:
s/\./ - /
"abc.def.ghi" ⇒ "abc - def.ghi"
To substitute all . but a leading one or the one in the extension, you can use the following:
s/(?!^)\.(?!\w+\z)/ - /g
Probably you will want to make sure that firs point is not the last. I mean if by any chance you will have nn_some_title_string.ext file name the script will not change a last dot.
$fileName = "nn.some.title.string.ext";
$fileName =~s/\.(?=\w+\.\w+)/-/;
print "FileName: " . $fileName ."\n";
Just try this for simple regex pattern.
my $str = "nn.some.title.string.ext";
$str=~s/^([^\.]*)\./$1-/i;
or
$str=~s/\./-/;
#^([^\.]*) Starting point upto the first dot
print $str;

Remove special characters using Pentaho - Replace in String

I wanted to remove the special characters like ! # # $ % ^ * _ = + | \ } { [ ] : ; < > ? / in a string field.
I used the "Replace in String" step and enabled the use RegEx. However, I do not know the right syntax that I will put in "Search" to remove all these characters from the string. If I only put one character in the "Search" it was removed from the string. How can I remove all of these??
This is the picture of how I did it:
As per documentation, the regex flavor is Java. You may use
\p{Punct}
See the Java regex syntax reference:
\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?#[]^_`{|}~

Regex Expression for textfield

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];

Regex help. I need ideas for solve the String Calculator kata with Groovy

I'm working on String Calculator code kata with Groovy.
There are a lot of scenarios that solve for achieve the solution:
I have:
//;\n1;2;3
//#\n1#2#3
//+\n1+2+3
//*\n1*2*3
//?\n1?2?3
I want:
1,2,3
My implementation:
String numbers = "//;\n1;2;3"
numbers.find(/\/\/\S[\n]/) { match ->
def delimeter = match[2]
numbers = numbers.minus(match).replaceAll(delimeter, ",")
}
With this solution I solved the first and second expressions, but I don't know how solve the others expressions.
java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
The problem is that we must also consider any symbol that match with the sintaxt of regular expressions like +, * or ?
Finally I have the solution:
String numbers = "//+\n1+2+3"
numbers.find(/(?s)\/\/(.*)\n/) { match ->
def delimeter = match[1] // also match[0][2]
numbers = numbers.minus(match[0]).replace(delimeter, ",")
}
An important point (?s):
In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.
Dotall mode can also be enabled via the embedded flag expression (?s)
But really the problem was here: .replace(delimeter, ",")
//(.)\n(\d)\1(\d)\1(\d)
Need to use links.
(.) - math thiw any character, and \1 - math thiw character on it\
For next example you can apply this: //\[(.*?)\]\\n(\d)\1(\d)\1(\d)
It math thiw
//[*]\n12**3
And last: //\[(.*?)\]\[(.*?)\]\\n(\d)\1(\d)\2(\d)
//[*][%%]\n1*2%%3
And finaly:
//\[(.*?)\](?:\[(.*?)\])?\\n(\d)\1(\d)(?:\2|\1)(\d)
I think it's can work ewerythere
P.S : (\d) you can replace what you want. I think you need (\d*)