I'm using the following code snippet:
Entity${0/(\w+)/\u\1/g}
This ensures the first character is uppercase and the rest is lowercase. How would I also ensure that hypens (-) and special characters are removed?
Thanks in advance.
Figured it out by doing the following:
Entity${0/(\w+)([-\s]*)/\u\1/g}
At the moment, this only removes hypens (-). I'd like to remove all characters except alphanumeric characters.
If there's a cleaner way, I'd be more than welcome to accept your answer instead.
Related
When someone by mistake enters many spaces between characters what I do is to replace all spaces with - but what if there are many spaces in between? for e.g:
User entered post title:
فارسی * Allposts---
When I convert the above example to user-friendly url (slug) I get this:
----فارسی---*-Allposts---
How to put one - for spaces and remove special characters and preserve utf-8 characters as well? The output I'm seeking for is as below:
فارسی-Allposts
Is there a way to handle it with regex? if positive, how?
EDIT:
Now I can manage multiple spaces as below:
$string = preg_replace('/\s+/', '-', $string);
but for special chars problem still remains.
Remove special characters: replace [\-\?\*] or whatever your blacklist characters are with empty string.
Convert strings of whitespace to a single - character: replace \s+ with -
Looks like you already figured out step 2. Make sure you do it second so you don't accidentally remove your own hyphens that you just inserted.
I'm trying to write a regex which allows alphabets, dot . and dashes - (for validation)
But couldn't find a valid regex which would do so, please help!
Thanks in advance
I think this will work for you
^[a-zA-Z-.]*$
any lowercase letter of the alphabet, any uppercase letter of the alphabet, dash as a group in any combination appearing 1 or many times
This character class should work for you:
[a-zA-Z.-]
Must Read: http://regular-expressions.info
Use this regex ([A-Za-z.-]) and test here http://www.rubular.com/r/H3Axvol13b
(?i)[a-z.-]
(?i) Will find any character no matter what case
I am trying a build a filter string for a Virtual table and would like underscore (_) to be a literal character and cannot figure out the escape character. Following are few that I tried and failed miserably:
VTAllDocs.Filter :='FILE_NAME like '+QuotedStr(MQDRegister.FieldByName('DOC_ID').AsString+'%\_REV%');
AND
VTAllDocs.Filter :='FILE_NAME like '+QuotedStr(MQDRegister.FieldByName('DOC_ID').AsString+'%[_]REV%');
AND
VTAllDocs.Filter :='FILE_NAME like '+QuotedStr(MQDRegister.FieldByName('DOC_ID').AsString+'*\_REV*');
AND
VTAllDocs.Filter :='(FILE_NAME like '+QuotedStr(MQDRegister.FieldByName('DOC_ID').AsString+'%^_REV%)+' ESCAPE "^")';
Really appreciate your help.
Thank you.
Never mind..
Contacted DevArt support desk and they said there is no escape character for underscore (_), so now I loop through the rows and do the checking manually.
I have seen the following on StackOverflow about URL characters:
There are two sets of characters you need to watch out for - Reserved and Unsafe.
The reserved characters are:
ampersand ("&")
dollar ("$")
plus sign ("+")
comma (",")
forward slash ("/")
colon (":")
semi-colon (";")
equals ("=")
question mark ("?")
'At' symbol ("#").
The characters generally considered unsafe are:
space,
question mark ("?")
less than and greater than ("<>")
open and close brackets ("[]")
open and close braces ("{}")
pipe ("|")
backslash ("\")
caret ("^")
tilde ("~")
percent ("%")
pound ("#").
I'm trying to code a URL so I can parse it using delimiters. They can't be numbers or letters though. Does anyone have a list of characters that are NOT Reserved but ARE safe to use?
Thanks for any help you can provide.
Don't bother trying to use safe/unreserved characters. Just use whatever delimiters you want and URLencode the whole thing. Then URL decode it on the other end and parse normally.
Is there a reason you can't just use the standard delimiter for URL parameters (&)? That is the most straightforward way to do it instead of trying to roll your own.
For example the standard URL syntax already allows for multi-valued paramaters natively. This is perfectly legal and doesn't require any trickery.
Somepage.aspx?parameterName=A¶meterName=B
The result is that the page would be passed "A,B" in the parameterName attribute.
There is a very similar question already. One of the solutions uses code like this one:
string.mb_chars.normalize(:kd).gsub(/[^x00-\x7F]/n, '').to_s
Which works wonders, until you notice it also removes spaces, dots, dashes, and who knows what else.
I'm not really sure how the first code works, but could it be made to strip only accents? Or at the very least be given a list of chars to preserve? My knowledge of regexps is small, but I tried (to no avail):
/[^\-x00-\x7F]/n # So it would leave the dash alone
I'm about to do something like this:
string.mb_chars.normalize(:kd).gsub('-', '__DASH__').gsub
(/[^x00-\x7F]/n, '').gsub('__DASH__', '-').to_s
Atrocious? Yes...
I've also tried:
iconv = Iconv.new('UTF-8', 'US-ASCII//TRANSLIT') # Also tried ISO-8859-1
iconv.iconv 'Café' # Throws an error: Iconv::IllegalSequence: "é"
Help please?
it also removes spaces, dots, dashes, and who knows what else.
It shouldn't.
string.mb_chars.normalize(:kd).gsub(/[^x00-\x7F]/n, '').to_s
You've mistyped, there should be a backslash before the x00, to refer to the NUL character.
/[^\-x00-\x7F]/n # So it would leave the dash alone
You've put the ‘-’ between the ‘\’ and the ‘x’, which will break the reference to the null character, and thus break the range.
I'd use the transliterate method. See http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate
It's not as neat as Iconv, but does what I think you want:
http://snippets.dzone.com/posts/show/2384