I have this regex expression(match either English or Hebrew chars, but not combined):
/^(?:[\u0590-\u05FF\uFB1D-\uFB40]+|[\w]+)$/i
It works ok, I just need to also add limitaion so no numbers would be allow.
This should match: abc, אבג
This should not be match: a1, 1b, aא,
The same limitation also need to be added to this regex expression:
/^(?:[\u0590-\u05FF\uFB1D-\uFB40 ]+|[\w ]+)$/i
Its purpose is the same as the first one, only that spaces are allowed.
This should match: abcx, abcx ascx, דגהק ,שגד דשגב
This should not be match: asaceדגעההת, ascasv אקיכרעקכ, as3, a3s, אב3ע
Also, if someone can help me to convert the new regex expression I requested,
and also this on:
/^05\d{8}$/i
from JavaScript to VB, I'd be most grateful.
Just use [A-Za-z] instead of \w if you don't want to allow numbers. If you are using the /i flag, you can also just use [a-z].
var regex = /^(?:[\u0590-\u05FF\uFB1D-\uFB40 ]+|[a-zA-Z ]+)$/i;
var texts = ["abcx", "abcx ascx", "דגהק" ,"שגד דשגב", "asaceדגעההת", "ascasv אקיכרעקכ", "as3", "a3s", "אב3ע"];
for(var i=0; i<texts.length; i++) {
var text = texts[i];
console.log(text + ": ", !!text.match(regex));
}
Related
So I'm completely new to regular expressions, and I'm trying to use Java's java.util.regex to find punctuation in input strings. I won't know what kind of punctuation I might get ahead of time, except that (1) !, ?, ., ... are all valid puncutation, and (2) "<" and ">" mean something special, and don't count as punctuation.
The program itself builds phrases pseudo-randomly, and I want to strip off the punctuation at the end of a sentence before it goes through the random process.
I can match entire words with any punctuation, but the matcher just gives me indexes for that word. In other words:
Pattern p = Pattern.compile("(.*\\!)*?");
Matcher m = p.matcher([some input string]);
will grab any words with a "!" on the end. For example:
String inputString = "It is a warm Summer day!";
Pattern p = Pattern.compile("(.*\\!)*?");
Matcher m = p.matcher(inputString);
String match = inputString.substring(m.start(), m.end());
results in --> String match ~ "day!"
But I want to have Matcher index just the "!", so I can just split it off.
I could probably make cases, and use String.substring(...) for each kind of punctuation I might get, but I'm hoping there's some mistake in my use of regular expressions to do this.
Java does support POSIX character classes in a roundabout way. For punctuation, the Java equivalent of [:punct:] is \p{Punct}.
Please see the following link for details.
Here is a concrete, working example that uses the expression in the comments
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexFindPunctuation {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\p{Punct}");
Matcher m = p.matcher("One day! when I was walking. I found your pants? just kidding...");
int count = 0;
while (m.find()) {
count++;
System.out.println("\nMatch number: " + count);
System.out.println("start() : " + m.start());
System.out.println("end() : " + m.end());
System.out.println("group() : " + m.group());
}
}
}
I would try a character class regex similar to
"[.!?\\-]"
Add whatever characters you wish to match inside the []s. Be careful to escape any characters that might have a special meaning to the regex parser.
You then have to iterate through the matches by using Matcher.find() until it returns false.
I would try
\W
it matches any non-word character. This includes spaces and punctuation, but not underscores. It’s equivalent to [^A-Za-z0-9_]
I was tring to find how to replace a regex, with keeping other regex part.
Example: Hi , how are you ? -> Hi, how are you?.
After studying a little i found that i could create groups, using "()", so just replaced the goup one, that was "(\s)".
String a = "Hi , how are you ?";
String p = "(\s)([,.!?\\-])";
System.out.println(a.replaceAll(p,"$2"));
//output: Hi, how are you?
I have messed around with special characters in regular expression for several hours now, and must admit that i give up.
Trying to make a password test function, that test for at least one of the following: lowercase, uppercase, integer and special character.
The special characters are "¤#+-£$!%*#?&().:;,_".
I have used this function to escape them:
//used to escape special characters [¤#+-£$!%*#?&().:;,_]
RegExp.escape = function(str) {
return String(str).replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
};
And tested the regular expression in these two tests:
var pattern1=/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[¤#\+-£\$\!%\*#\?&\(\)\.\:;,_]).{8,}$/g;
var regexVal1=pattern1.test(password);
var pattern2=new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[¤#\+-£\$\!%\*#\?&\(\)\.\:;,_]).{8,}$","g");
var regexVal2=pattern2.test(password);
The results are:
var password="AaBbCcDd";//both regexVal1 and regexVal2 is false
var password="AaBbCcDd90";//both regexVal1 and regexVal2 is true
var password="AaBbCcDd90#¤";//both regexVal1 and regexVal2 is true
The result from var password="AaBbCcDd90"; should be "false"!
The question is: What am i doing wrong??
The reason is - has special meaning in character class. So \+-£ inside it means "all characters in table of Unicode codes from '+' up to '£'".
So you need escape '-' there.
And yes, you don't need to escape all other characters there
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[¤#+\-£$!%*#?&().:;,_]).{8,}$/g
should be fine for you
enough you add "\" before "+" and "-";
var Regex1 =^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[¤#\+\-\£\$\!%\*#\?&\(\)\.\:;,_]).{8,}$
easy way for test your regular expressions is use this website: https://regex101.com/
this example is also good:
Use RegEx To Test Password Strength In JavaScript
var mediumRegex = new RegExp("^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})");
https://www.thepolyglotdeveloper.com/2015/05/use-regex-to-test-password-strength-in-javascript/
I have a returned string consisting of multiple classes:
"xxx1sbu xxx2sdf xxx1sef xxx1sb1 xxx1su xxx1s1 dxxx1s xxx1sdfg xxx1sbf"
I need the regex to search the string and find the classes based on the following criteria:
1) class begins with 'xxx1s'
2) class is no longer than 7 characters (letters and numbers) excluding a following space ('xxx1sbu')
3) if a space follows the class ('xxx1sbu ') then it is also found so that it can be removed.
I then use the regex to replace the found classes to
var classesReplaced = classesString.replace(regex, "")
The string should then look as follows:
"xxx2sdf dxxx1s xxx1sdfg"
So far the best I have come up with is:
RegExp: /\bxxx1s([a-z1-9])([a-z1-9])(\s)|\bxxx1s([a-z1-9])([a-z1-9])\b/g
pattern: \bxxx1s([a-z1-9])([a-z1-9])(\s)|\bxxx1s([a-z1-9])([a-z1-9])\b
I also tried to use javascript to build the expression but it keeps stripping off the '\b':
var classId = 'xxx1s';
var regex2 = new RegExp('\b'+ classId +'([a-z1-9])([a-z1-9])(\s)|\b'+ classId +'([a-z1-9])([a-z1-9])\b','g');
Is there a better way to write this?? My understanding of regex isn't great!
Thank you in advance
I think this is the command and regex you need:
NewString = OldString.replace( /\bxxx1s\S{0,2}\b\s?/, "" );
Here's how that breaks down:
\b is the opening word border for the section you want to replace.
xxx1s is the base text you want to remove.
\S{0,2} (upper-case S) finds any non-white-space between 0 and 2 characters long; added to the item above, it makes sure that only items of 7 or fewer characters are found.
\b is the closing word border.
\s? (lower-case s) finds the trailing space; the ? makes it optional so the last item in the series won't be skipped.
Here is the regex you should use:
var regex = /((?:\s*(xxx1s)\w{1,2})(?=\W))|(xxx1sbu )/g;
I have the below String value to be displayed in text area and i want to remove the first characters ##*n|n from the string .
The string is as follows :
Symbol-001
##*n|nClaimant Name
##*n|nTransaction
I have used the below code to deal with removing the special characters
var paramVal1 = parent.noteText; //paramVal1 will have the string now
var pattern = /[##*n|n]/g;
var paramVal1 = paramVal1.replace(pattern,'');
document.getElementById("txtNoteArea").value = paramval1;//appending the refined string to text area
For the above used code am getting the out put string as below
Symbol-001
|Claimat Name //here 'n' is missing and i have an extra '|' character
|Transactio //'n' is missing here too and an extra '|' character
Kindly help to remove the characters ##*n|n without affecting the other values
What your regex is saying is "remove any of the following characters: #|*n". Clearly this isn't what you want!
Try this instead: /##\*n\|n/g
This says "remove the literal string ##*n|n". The backslashes remove the special meaning from * and |.
You are using regular expression reserved chars in your pattern, you need to escape them
You can use this expression:
var pattern = /[\#\#\*n\|n]/g;
i think use this /[##*n\|n]/g regEx
If you want to replace the first occurrence as you say on your question, you don't need to use regex. A simple string will do, as long as you escape the asterisk:
var str = "Symbol-001 ##*n|nClaimant Name ##*n|nTransaction";
var str2 = str.replace("##\*n|n", ""); //output: "Symbol-001 Claimant Name ##*n|nTransaction"
If you want to replace all the occurrences, you can use regex, escaping all the characters that have a special meaning:
var str3 = str.replace(/\#\#\*n\|n/g, ""); //output: "Symbol-001 Claimant Name Transaction"
Have a look at this regex builder, might come in handy - http://gskinner.com/RegExr/
I have a textbox where a user puts a string like this:
"hello world! I think that __i__ am awesome (yes I am!)"
I need to create a correct URL like this:
hello-world-i-think-that-i-am-awesome-yes-i-am
How can it be done using regular expressions?
Also, is it possible to do it with Greek (for example)?
"Γεια σου κόσμε"
turns to
geia-sou-kosme
In other programming languages (Python/Ruby) I am using a translation array. Should I do the same here?
Try this:
function doDashes(str) {
var re = /[^a-z0-9]+/gi; // global and case insensitive matching of non-char/non-numeric
var re2 = /^-*|-*$/g; // get rid of any leading/trailing dashes
str = str.replace(re, '-'); // perform the 1st regexp
return str.replace(re2, '').toLowerCase(); // ..aaand the second + return lowercased result
}
console.log(doDashes("hello world! I think that __i__ am awesome (yes I am!)"));
// => hello-world-I-think-that-i-am-awesome-yes-I-am
As for the greek characters, yeah I can't think of anything else than some sort of lookup table used by another regexp.
Edit, here's the oneliner version:
Edit, added toLowerCase():
Edit, embarrassing fix to the trailing regexp:
function doDashes2(str) {
return str.replace(/[^a-z0-9]+/gi, '-').replace(/^-*|-*$/g, '').toLowerCase();
}
A simple regex for doing this job is matching all "non-word" characters, and replace them with a -. But before matching this regex, convert the string to lowercase. This alone is not fool proof, since a dash on the end may be possible.
[^a-z]+
Thus, after the replacement; you can trim the dashes (from the front and the back) using this regex:
^-+|-+$
You'd have to create greek-to-latin glyps translation yourself, regex can't help you there. Using a translation array is a good idea.
I can't really say for Greek characters, but for the first example, a simple:
/[^a-zA-Z]+/
Will do the trick when using it as your pattern, and replacing the matches with a "-"
As per the Greek characters, I'd suggest using an array with all the "character translations", and then adding it's values to the regular expression.
To roughly build the url you would need something like this.
var textbox = "hello world! I think that __i__ am awesome (yes I am!)";
var url = textbox.toLowerCase().replace(/([^a-z])/, '').replace(/\s+/, " ").replace(/\s/, '-');
It simply removes all non-alpha characters, removes double spacing, and then replaces all space chars with a dash.
You could use another regular expression to replace the greek characters with english characters.