regex

Regex for matching multilingual numbers not detecting Chinese numbers

懵懂的女人 提交于 2021-02-07 13:33:24
问题 I have a method which detects whether a String is a number: public static boolean isNumber(String num) { return num.matches("(\\p{N})+"); } The above method is successfully matching English, Hindi, Arabic numbers but fails to match Chinese numbers: 三十萬零二百五十 etc. Is it possible to create a regex which can match a number from any language(or major languages)? edit: the number won't be a decimal, it will be used to validate a phone number. 回答1: as some have already pointed out in the comments,

R remove repeated digit sequences

十年热恋 提交于 2021-02-07 13:27:05
问题 I am trying to remove all digits in a string except the first set of digits. So in other words, all repeating sets of digits, there could be 1 sets or 10+ sets in the string but I only want to keep the first set along with the rest of the string. For example, the following string: x <- 'foo123bar123baz123456abc1111def123456789' The result would be: foo123barbazabcdef I am have tried using gsub and replacing \d+ with an empty string but this replaces all digits in the string, I have also tried

How does negative lookahead with asterisks work?

拈花ヽ惹草 提交于 2021-02-07 12:40:12
问题 I'm trying to understand why I'm not getting the expected results from a regex. I already know what is negative lookahead (apparently not :-)) And also that asterisks is zero or more times of repeats. Looking at this regex : a(?![^3]) This will match a which isn't followed by a non-3 after it. So looking at this test string , the bold part is a match: a 3333335 Ok Also- if I change the regex to : a(?![^3]+) //notice "+" It will still match : a 3333335 This will match a which isn't followed by

How does negative lookahead with asterisks work?

末鹿安然 提交于 2021-02-07 12:39:22
问题 I'm trying to understand why I'm not getting the expected results from a regex. I already know what is negative lookahead (apparently not :-)) And also that asterisks is zero or more times of repeats. Looking at this regex : a(?![^3]) This will match a which isn't followed by a non-3 after it. So looking at this test string , the bold part is a match: a 3333335 Ok Also- if I change the regex to : a(?![^3]+) //notice "+" It will still match : a 3333335 This will match a which isn't followed by

Java - Parsing strings - String.split() versus Pattern & Matcher

℡╲_俬逩灬. 提交于 2021-02-07 12:19:34
问题 Given a String containing a comma delimited list representing a proper noun & category/description pair, what are the pros & cons of using String.split() versus Pattern & Matcher approach to find a particular proper noun and extract the associated category/description pair? The haystack String format will not change. It will always contain comma delimited data in the form of PROPER_NOUN|CATEGORY/DESCRIPTION Common variables for both approaches: String haystack="EARTH|PLANET/COMFORTABLE,MARS

Counting overlapping matches with Regex in C# [duplicate]

让人想犯罪 __ 提交于 2021-02-07 12:12:00
问题 This question already has answers here : Overlapping matches in Regex (3 answers) Closed 7 months ago . The following code evaluates 2 instead of 4: Regex.Matches("020202020", "020").Count; I'm guessing the regex starts looking for the next match from the end of the previous match. Is there any way to prevent this. I have a string of '0's and '2's and I'm trying to count how many times I have three '2's in a row, four '2's in a row etc. 回答1: This will return 4 as you expect: Regex.Matches(

Counting overlapping matches with Regex in C# [duplicate]

感情迁移 提交于 2021-02-07 12:10:14
问题 This question already has answers here : Overlapping matches in Regex (3 answers) Closed 7 months ago . The following code evaluates 2 instead of 4: Regex.Matches("020202020", "020").Count; I'm guessing the regex starts looking for the next match from the end of the previous match. Is there any way to prevent this. I have a string of '0's and '2's and I'm trying to count how many times I have three '2's in a row, four '2's in a row etc. 回答1: This will return 4 as you expect: Regex.Matches(

Rails custom route with constraints - regexp anchor characters are not allowed in routing requirements

╄→гoц情女王★ 提交于 2021-02-07 11:29:11
问题 I have the following route: get 'users/:user_id/:name', to: 'profiles#show', :constraints => { :name => /[a-zA-Z0-9_]+$/ }, as: 'user_profile' Which produces the error: Regexp anchor characters are not allowed in routing requirements: /[a-zA-Z0-9_]+$/ So I get that the ^ character isn't allowed, but not sure what character is producing this particular routing error. 回答1: In regex we have two anchors: Beginning of line/string ^ End of line/string $ Try to remove $ from the pattern and you

Replace certain values based on pattern and extract substring in pandas

流过昼夜 提交于 2021-02-07 10:55:49
问题 Pandas Dataframe with col1 that contains various dates col1 Q2 '20 Q1 '21 May '20 June '20 25/05/2020 Q4 '20+Q1 '21 Q2 '21+Q3 '21 Q4 '21+Q1 '22 I want to replace certain values in col1 that match a pattern. For the values that contain 2 quarters with "+" I want to return a season in string plus the first year contained in the pattern. I want to leave the other values as they are. For example: 1) Q4 '20+Q1 '21 should be 'Winter 20' 2) Q2 '21+Q3 '21 should be 'Summer 21' 3) Q4 '21+Q1 '22 should

How to replace multiple spaces with single space?

旧街凉风 提交于 2021-02-07 10:55:27
问题 I am developing web app using C#. I want to replace multiple spaces with single space in between string. I tried with normal string replace function, but it was not helpful. It is possible with Regular Expression, but I don't have clear idea about that. Please provide a example code for the following string: Actual String: Have a Nice Day ! !! Needed: Have a Nice Day !!! 回答1: You can match the following: @"\s+" and replace with: " " Regex.Replace("Have a Nice Day ! !!", @"\s+", " "); 回答2: See