regex

PREG_MATCH check all words and condition

落花浮王杯 提交于 2021-02-05 07:39:16
问题 I've written a regular expression which seraches the search terms in OR condition, such that provided three words have in string irrespective of their order. Now i want to just put an AND condition as I want to get ALL THREE words simulatniously in a string with different order. Here's my preg_match() regular expresison. $myPregMatch = (preg_match("/\b^(Word1|Word2|Word3)\b/", "Word4 Word2 Word1 Word3 Word5 Word7")); if ($myPregMatch){ echo "FOUND !!!"; } I want to find in string "Word4 Word2

Rename files using bash/regex?

旧城冷巷雨未停 提交于 2021-02-05 07:33:13
问题 I have files named: test-12.5.0_567-release.apk I want them to look like: test-release.apk I realized I can do it with bash: for file in *release.apk; do mv "$file" "`basename $file SOMETHING`NEW_FILE_NAME"; done It needs some regex I guess ? How would it look like ? Thanks ! 回答1: You can do: for file in *release.apk; do mv "$file" "${file/-*-/-}" done 回答2: Alternatively you can use this: for file in *release.apk; do mv "$file" "${file%-*-*}-release.apk" I'm removing -12.5.0_567-release.apk

Remove comma when between quotes from batch or PoSh

折月煮酒 提交于 2021-02-05 07:33:11
问题 I have a CSV file with a content like: A,B,C D,"E,F",G H,I,"J,K,L" I need to remove the commas when between quotes (also remove the quotes, but that is not so important): A,B,C D,EF,G H,I,JKL I looked at PoSh -replace operator but I can't get it to capture multiple group values: PS >"D,`"E,F`",G" -replace "`"((?:[^,`"]+)\,?)+`"", '$1' D,F,G as you can see when the group is repeated, only the last value captured is preserved. Is there a way to do the transformation I want? https://regex101.com

How to extract a number before a certain words?

血红的双手。 提交于 2021-02-05 07:32:32
问题 There is a sentence "i have 5 kg apples and 6 kg pears". I just want to extract the weight of apples. So I use sentence = "I have 5 kg apples and 6 kg pears" number = re.findall(r'(\d+) kg apples', sentence) print (number) However, it just works for integer numbers. So what should I do if the number I want to extract is 5.5? 回答1: The regex you need should look like this: (\d+.?\d*) kg apples You can do as follows: number = re.findall(r'(\d+.?\d*) kg apples', sentence) Here is an online

Why is preg_match behaving differently to preg_replace (resulting in different matches) in php?

孤街浪徒 提交于 2021-02-05 07:31:42
问题 Given the following string and regular expression, the resulting behavior is something I don't understand. preg_match delivers what I am expecting while preg_replace doesn't make sense to me. $string = 'aaa [Ticket#RS-123456] äüö [xxx] ccc ddd'; $re = '@(.*)?(\[Ticket\#)(.*)(\])(.*)?@siU'; What I finally need in this example is the string RS-123456 (or whatever string would be at this position). This string should match at the 3rd position ($3), if I don't completely misunderstand regular

Regex text between two strings

泄露秘密 提交于 2021-02-05 07:29:24
问题 I am trying to extract data fields from PDF texts using regex. The text is: "SAMPLE EXPERIAN CUSTOMER\n2288150 - EXPERIAN SAMPLE REPORTS\nData Dictionary Report\nFiltered By:\nCustom Selection\nMarketing Element:\nPage 1 of 284\n2014-11-11 21:52:01 PM\nExperian and the marks used herein are service marks or registered trademarks of Experian.\n© Experian 2014 All rights reserved. Confidential and proprietary.\n**Data Dictionary**\nDate of Birth is acquired from public and proprietary files.

Escape Java RegExp Metacharacters

China☆狼群 提交于 2021-02-05 07:25:46
问题 I'm trying to escape a RegExp metacharacter in Java. Below is what I want: INPUT STRING: "This is $ test" OUTPUT STRING: "This is \$ test" This is what I'm currently doing but it's not working: String inputStr= "This is $ test"; inputStr = inputStr.replaceAll("$","\\$"); But I'm getting wrong output: "This is $ test$" 回答1: You'll need: inputStr.replaceAll("\\$", "\\\\\\$"); The String to be replaced needs 2 backslashes because $ has a special meaning in the regexp. So $ must be escaped, to

Matching when an arbitrary pattern appears multiple times

依然范特西╮ 提交于 2021-02-05 07:14:25
问题 I'm trying to write a regex that will succeed when an arbitrary pattern of length 2 or greater appears more than once in a phrase. Essentially, I would like to use a capture group in the search. Something like this, but generalized to match any phrase, not just foo /foo.*foo/ For example, Should match: abcdabcd ('abcd' is repeated) foobarfoo ('foo' is repeated) mathematics ('mat' is repeated) Should not match: bar (no repetition of any pattern) foo ('o' is repeated but it is not length>=2)

Regular Expression Log Parsing

為{幸葍}努か 提交于 2021-02-05 07:14:05
问题 I'm using regular expressions to parse logs. I was previously reading the File into a string array, and then iterating through the string array appending if I don't match the timestamp, otherwise I add the line I'm iterating on to a variable and continue the search. Once I get a complete log entry, I use another regular expression to parse it. Scanning file try { List<String> lines = Files.readAllLines(filepath); Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d

Regular expression quoting in Python

橙三吉。 提交于 2021-02-05 07:12:26
问题 How should I declare a regular expression? mergedData = re.sub(r'\$(.*?)\$', readFile, allData) I'm kind of wondering why this worked. I thought that I need to use the r'' to pass a regular expression. mergedData = re.sub("\$(.*?)\$", readFile, allData) What does "\$" result in in this case? Why? I would have thought "$" . 回答1: I thought that I need to user the r'' to pass a regular expression. r before a string literal indicates raw string, which means the usual escape sequences such as \n