character-class

How do you use a plus symbol with a character class as part of a regular expression?

試著忘記壹切 提交于 2021-02-04 15:58:26
问题 in cygwin, this does not return a match: $ echo "aaab" | grep '^[ab]+$' But this does return a match: $ echo "aaab" | grep '^[ab][ab]*$' aaab Are the two expressions not identical? Is there any way to express "one or more characters of the character class" without typing the character class twice (like in the seconds example)? According to this link the two expressions should be the same, but perhaps Regular-Expressions.info does not cover bash in cygwin. 回答1: grep has multiple "modes" of

How do you use a plus symbol with a character class as part of a regular expression?

a 夏天 提交于 2021-02-04 15:57:48
问题 in cygwin, this does not return a match: $ echo "aaab" | grep '^[ab]+$' But this does return a match: $ echo "aaab" | grep '^[ab][ab]*$' aaab Are the two expressions not identical? Is there any way to express "one or more characters of the character class" without typing the character class twice (like in the seconds example)? According to this link the two expressions should be the same, but perhaps Regular-Expressions.info does not cover bash in cygwin. 回答1: grep has multiple "modes" of

Why is /[\w-+]/ a valid regex but /[\w-+]/u invalid?

北战南征 提交于 2021-01-29 11:20:57
问题 If I type /[\w-+]/ in the Chrome console, it accepts it. I get a regex object I can use to test strings as usual. But if I type /[\w-+]/u , it says VM112:1 Uncaught SyntaxError: Invalid regular expression: /[\w-+]/: Invalid character class . In Firefox, /[\w-+]/ works fine, but if I type /[\w-+]/u in the console, it just goes to the next line as if I typed an incomplete statement. If I try to force it to create the regex by running eval('/[\w-+]/u') , it tells me SyntaxError: invalid range in

Is [\s\S] same as . (dot)?

别等时光非礼了梦想. 提交于 2020-12-13 09:36:20
问题 When we include shorthand for character class and negated-character class in same character class, is it same as dot . which mean any character ? I did a test on regex101.com and every character matched. Is [\s\S] [\w\W] and [\d\D] same as . ? I want to know if this behavior is persistent in web's front and backend languages like Javascript, Php, Python and others. 回答1: "No" it is not the same. It has an important difference if you are not using the single line flag (meaning that . does not

Is [\s\S] same as . (dot)?

爱⌒轻易说出口 提交于 2020-12-13 09:36:06
问题 When we include shorthand for character class and negated-character class in same character class, is it same as dot . which mean any character ? I did a test on regex101.com and every character matched. Is [\s\S] [\w\W] and [\d\D] same as . ? I want to know if this behavior is persistent in web's front and backend languages like Javascript, Php, Python and others. 回答1: "No" it is not the same. It has an important difference if you are not using the single line flag (meaning that . does not

Removing all punctuation except - and _ from a java string using RegEx

社会主义新天地 提交于 2019-12-20 03:28:08
问题 I am trying to replace all punctuation except the - and _ using a method I found here, but I can only get it to work on " using the exact code as posted which used a negative lookahead: (?!")\\p{punct} //Java example: String string = ".\"'"; System.out.println(string.replaceAll("(?!\")\\p{Punct}", "")); I tried: name = name.replaceAll("(?!_-)\\p{Punct}", ""); // which just replaces all punctuation. name = name.replaceAll("(?!\_-)\\p{Punct}", ""); // which gives an error. Thanks. 回答1: Use a

How word character is interpreted in character class?

时光总嘲笑我的痴心妄想 提交于 2019-12-19 10:17:38
问题 \w - stands for [A-Za-z0-9_] Character class But i am not able to understand how it is interpreted inside character class. So when i use [\w-~] let test = (str) => /^[\w-~]+$/.test(str) console.log(test("T|")) it fails for T| but when i am using [A-Za-z0-9_-~] let test = (str) => /^[A-Za-z0-9_-~]+$/.test(str) console.log(test("T|")) it results in true, i am not able to understand how these two expressions are different from each other ? 回答1: I believe that the main difference between both

Replace all characters not in range (Java String)

半腔热情 提交于 2019-12-17 22:45:52
问题 How do you replace all of the characters in a string that do not fit a criteria. I'm having trouble specifically with the NOT operator. Specifically, I'm trying to remove all characters that are not a digit, I've tried this so far: String number = "703-463-9281"; String number2 = number.replaceAll("[0-9]!", ""); // produces: "703-463-9281" (no change) String number3 = number.replaceAll("[0-9]", ""); // produces: "--" String number4 = number.replaceAll("![0-9]", ""); // produces: "703-463-9281

Character class subtraction, converting from Java syntax to RegexBuddy

孤街浪徒 提交于 2019-12-17 09:53:39
问题 Which regular expression engine does Java uses? In a tool like RegexBuddy if I use [a-z&&[^bc]] that expression in Java is good but in RegexBuddy it has not been understood. In fact it reports: Match a single character present in the list below [a-z&&[^bc] A character in the range between a and z : a-z One of the characters &[^bc : &&[^bc Match the character ] literally : ] but i want to match a character between a and z intersected with a character that is not b or c 回答1: Like most regex

What built-in regex character classes are supported Java

∥☆過路亽.° 提交于 2019-12-12 07:59:25
问题 ...when used in patterns like "\\p{someCharacterClass}" . I've used/seen some: Lower Upper InCombiningDiacriticalMarks ASCII What is the definitive list of all supported built-in character classed? Where is it documented? What are the exact meanings? Edited... There seem to be a lot of "RTFM" answers refering to the javadoc for Pattern . That's the first place I looked before asking this question. Just so everyone is clear, the javadoc for Pattern makes no mention of any of the classes listed