regex

How to match all numerical characters and some single characters using regex

不羁的心 提交于 2021-02-05 11:32:08
问题 How can I match all numbers along with specific characters in a String using regex? I have this so far if (!s.matches("[0-9]+")) return false; I don't understand much regex, but this matches all characters from 0-9 and now I need to be able to match other specific characters, for example "/", ":", "$" 回答1: You can use this regex by including those symbols in a character class : s.matches("[0-9$/:]+") Read more about character class 回答2: You can add the other characters that you need to match

Regex to select a file extension

China☆狼群 提交于 2021-02-05 11:31:28
问题 I have an apk file say MyApp.apk. I was trying to strip the .apk extension using the strip function in python. But the problem is, If my applications name is WhatsApp.apk then the function strips the letters pp also and outputs WhatsA . What Regex should I use to strip exactly the .apk away? 回答1: Why use regex? If you only want the filename then this code will do filename = 'MyApp.apk' filename = filename.rsplit('.', 1)[0] print filename output: MyApp 回答2: import re x="MyApp.apk" print re.sub

Why does string.match(…)[0] throws an exception?

∥☆過路亽.° 提交于 2021-02-05 11:19:26
问题 I'm trying to pull the first occurence of a regex pattern from a string all in one statement to make my code look cleaner. This is what I want to do: var matchedString = somestring.match(/some regex/g)[0]; I would expect this to be legal but it throws an exception: Exception: somestring.match(...) is null It seems like JS is trying to index the array before match is finsihed, as the array does provide atleast one match, so I don't expect it to be null. I would like some insight in why it

Pattern for apostrophe inside quotes

一世执手 提交于 2021-02-05 11:14:11
问题 I am looking for a pattern that can find apostrophes that are inside single quotes. For example the text Foo 'can't' bar 'don't' I want to find and replace the apostrophe in can't and don't, but I don't want to find the single quotes I have tried something like (.*)'(.*)'(.*)' and apply the replace on the second matching group. But for text that has 2 words with apostrophes this pattern won't work. Edit: to clarify the text could have single quotes with no apostrophes inside them, which

matching whole words while ignoring affixes of words using regex

久未见 提交于 2021-02-05 11:12:33
问题 I am learning a new language and I have created a DB with aprox. 2500 words and 2500 examples of the words. I created a PHP/MySQL web UI with basically shows pictures for each word and when you click them it will play the audio of the word. There is also a context menu to trigger a pop up div that matches and displays all examples where the word occurs. I have been using REGEXP '[[:<:]]$word[[:>:]]' but there are several prefixes/suffixes of words that I want to filter out that do not add any

PHP preg_match() or strpos to check if string starts with substring

穿精又带淫゛_ 提交于 2021-02-05 11:09:43
问题 I am changing views of a page depending the $_GET['code'] it can equal a series of different codes but they will all start with either Red , Town or Bearing , then a series of characters. For example, the following would all trigger the condition RedWings-223123-NY Townmansion-2341322-KY BearingWays-23422-DC I have tried <?php if(preg_match("%(?=.*Bear)(?=.*Red)(?=.*Town)%", $_GET['code'])): ?> <span> new view </span> <?php endif ?> and <?php if( strpos($_GET['code']), "Red" ) !== false) ||

Regex asterisk usage

依然范特西╮ 提交于 2021-02-05 10:48:07
问题 According to MDN x* : *Matches the preceding item x 0 or more times.* Essentially the preceding characters should be completely optional. The string will be matched whether they exist or not. So why is it that: 1. var text = "foobar"; var re = /q*/; var found = text.match(re); console.log(found); // prints '["", index: 0, input: "foobar"]' but var re = /qz*/; console.log(found); // prints 'null' Both expressions equally don't exist and thus should be matched 0 times and '""' should be

extract part of a path with filename with php

岁酱吖の 提交于 2021-02-05 10:46:05
问题 I need to extract a portion of urls using php. The last 6 segments of the url are the part I need. The first part of the url varies in length and number of directories. So if I have a url like this: https://www.random.ccc/random2/part1/part2/part3/2017/08/file.txt or this: https://www.random.vov/part1/part2/part3/2016/08/file.pdf What I need is this: /part1/part2/part3/2017/08/file.txt or this: /part1/part2/part3/2016/08/file.pdf I have tried this: $string = implode("/",array_slice(explode("/

Using regex in Grep for Windows command line

柔情痞子 提交于 2021-02-05 10:10:22
问题 I want to capture all lines which contain exactly 3 fields, where a field is any string (possibly empty) followed by a | (and there may be some final text at the end of the line). I managed to build a regex which seems to do exactly what I want ^(?:[^\|]*\|){3}[^\|]*$ and when I try it on 101regex it seems to work just fine. However, I am having problems to run this regex on the Windows command line via grep and I guess it has something to do with the proper escaping. I tried grep -E '^^(?:[^

Using regex in Grep for Windows command line

梦想的初衷 提交于 2021-02-05 10:02:50
问题 I want to capture all lines which contain exactly 3 fields, where a field is any string (possibly empty) followed by a | (and there may be some final text at the end of the line). I managed to build a regex which seems to do exactly what I want ^(?:[^\|]*\|){3}[^\|]*$ and when I try it on 101regex it seems to work just fine. However, I am having problems to run this regex on the Windows command line via grep and I guess it has something to do with the proper escaping. I tried grep -E '^^(?:[^