Questions tagged [regex]
92242 questions
1
votes
3
answer
2.7k
Views
Python how convert single quotes to double quotes to format as json string
I have a file where on each line I have text like this (representing cast of a film):
[{'cast_id': 23, 'character': 'Roger 'Verbal' Kint', 'credit_id': '52fe4260c3a36847f8019af7', 'gender': 2, 'id': 1979, 'name': 'Kevin Spacey', 'order': 5, 'profile_path': '/x7wF050iuCASefLLG75s2uDPFUu.jpg'}, {'cast...
1
votes
2
answer
377
Views
How to split a string with regexp without keeping capture groups?
I want to split text with a regular expression with backreference in Python.
rexp = re.compile(r'([`]{1,})ABC\1')
rexp.split('blahblah``ABC``blahblah')
I got ['blahblah', '``', 'blahblah'] but expected ['blahblah', 'blahblah'].
How can I split string without keeping capture groups?
1
votes
2
answer
132
Views
Python re.sub() optimization
I have a python list with each string being one of the following 4 possible options like this (of course the names would be different):
Mr: Smith\n
Mr: Smith; John\n
Smith\n
Smith; John\n
I want these to be corrected to:
Mr,Smith,fname\n
Mr,Smith,John\n
title,Smith,fname\n
title,Smith,John\n
Easy en...
1
votes
2
answer
54
Views
Searching for a whole word that contains leading or trailing special characters like - and = using regex in python
I am trying to know a position of a string (word) in a sentence. I am using the function below. This function is working perfectly for most of the words but for this string GLC-SX-MM= in the sentence I have a lot of GLC-SX-MM= in my inventory list there is no way to get the match. I tryied scaping -...
1
votes
3
answer
66
Views
Python regular expression to split parameterized text file
I'm trying to split a file that contains 'string = float' format repeatedly.
Below is how the file looks like.
+name1 = 32 name2= 4
+name3 = 2 name4 = 5
+name5 = 2e+23
...
And I want them to put it a dictionary.
Like...
a={name1:32, name2:4, name3:2, name4:5, name5:2e+23}
I'm new to regu...
1
votes
1
answer
167
Views
Biq Query regex_replace error (\? vs \\?)
I am having issues understanding what's wrong with this regex: \?.*
select REGEXP_REPLACE(longstringcolumn, '\?.*', '') as newstring from tablename
My example string aka 'longstring' has '?' character, and I am trying to match everything trailing '?' (including '?' itself).
I have checked my regexp...
-2
votes
1
answer
12
Views
How can I select comma but if it with another, exclude in regex
I would like to make JSON formatter
want to select comma in JSON
and also want to deselect if comma with bracket
{
'name':'s1',
0
votes
0
answer
12
Views
Replacing character with a string in Nginx
Is there a way I can replace # with '?somecharacter' ?
What i'm trying to do is convert my URL from https://localhost/#afljkdhslfjdsaf to something linke https://localhost/?var=afljkdhslfjdsaf.
While i know how to access query paramerts inside nginx block i've not idea how to capture the # and repla...
1
votes
2
answer
518
Views
Regex Pattern for All Discord Tag Types
doing some discord.js bot development and I'm trying to create a regex that will filter out all types of discord tags (aka tagged users and custom emotes). I've tried a few different things, but none that will capture every case. The 5 main styles of tag are:
(User)
(User)
(Emote without number in n...
1
votes
2
answer
677
Views
Template Literals with Javascript Replace? [duplicate]
This question already has an answer here:
Template literal inside of the RegEx
1 answer
am I able to use template literals with javascript replace regex?
I have something like this
const regex = '[email protected]#$%^&*()_' ;
const data = '$88.10';
const result = data.replace(`[^${regex}/gi]$`, '');
nothing is g...
1
votes
2
answer
166
Views
Swift - Splitting strings with regex - ignoring search string
There is a clever 1st answer given here for splitting swift string with a regex expression
split string answer
However it keeps the searched text within the array of answers. I'm trying to do a similar thing, but ignoring the characters acting as separators (e.g. just like the swift split function,...
1
votes
4
answer
819
Views
MongoDB regex string startswith and endswith [duplicate]
This question already has an answer here:
Regex matching beginning AND end strings
4 answers
Regular Expression to find string starts with letter and ends with slash /
3 answers
So i have something that looks like this
db.usuarios.insert
(
[
{
'nome' : 'neymala',
'idade' : 40,
'status' : 'solteir...
1
votes
4
answer
68
Views
Replacing “../../../” using sed
I have a text file needs to edit. The first field contains the string
'../../../'
and I want to replace with
'/home'
. I tried using sed, but .. and / are special characters and I am a little bit stumbled how to match these patterns. I tried using something like (..), but it seems not working for...
1
votes
1
answer
69
Views
Solve Catastrophic Backtracking in my regex detecting links?
I have an regex expression to find links in texts:
(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^'\'\\n\\r()]+|\\(([^'\'\\n\\r()]+|(\\([^'\'\\n\\r()]+\\)))*\\))+(?:\\(([^'\'\\n\\r()]+|(\\([^'\'\\n\\r()]+\\)))*\\)|[^'\'\\n\\r`!()\\[\\]{};:'.,?\u00AB\u00BB\u201C\u201D\u2018\u201...
0
votes
0
answer
16
Views
Lookahead works with other languages but not in Shell
I have a string on $OUTPUT variable and I wanna get the only the backup directory. I've tested my regex on regex101 and with other languages, and it works, but in Shell, it doesn't.
OUTPUT='Backup created at /data/bkp/bkp-2019_04_25/dremio_backup_2019-04-25_15.56, tables 25, uploaded files 0'
REGEX=...
0
votes
0
answer
10
Views
How to allow a number between 0-12 and also have additional pattern aside it
I am trying to figure out a pattern for html and I can't get it to work...
As the result I am looking for something lower than this number: '12.50'
I am expecting the first number to be a number between 0-12, after that the '.' and after that a number between 0-5 and at the end just the 0.
It works...
1
votes
4
answer
67
Views
Regex to delete all characters after space // Android Java
I try to convert following string 1.00 h. to 1.00 with RegEx but I'm new with it and need your help. I've tried as follows
seanceLengthValue = Integer.parseInt(
currentService.getSeanceLength().replaceAll('\\D\\w+',''));
1
votes
3
answer
33
Views
non-greedy search for redundant values in string
Basically I have this string and I want to get only a distinct image filename.
/mPastedImg_Time1469244713469.png>xResourceFileName=mPastedImg_Time1469244713469.png&
I have this regex code but it does not seem to work.
[^\/]*?_Time[0-9]{13}\.\w{3,4}\&
My expected output is:
mPastedImg_Time14692...
1
votes
1
answer
33
Views
python:How to extract a word before and after the match using regex
Cosnider the follwing data as sample
input_corpus = 'this is an example.\n I am trying to extract it.\n'
I am trying to extract exactly 2 words before and after .\n with the following code
for m in re.finditer('(?:\S+\s+){2,}[\.][\n]\s*(?:\S+\b\s*){0,2}',input_corpus):
print(m)
Expected output :
an...
1
votes
1
answer
36
Views
Converting cell values to snake_case
I have some data cleansing task. I have a column which start from H6 and further down from their. The column contains data which was supposed to be in snake_case, but is not the case. The cell values are of the form:
With camel case: 'CamelCase'
With spaces: 'Spaced Value'
With some initial call cap...
1
votes
2
answer
53
Views
Is there a regular expression for finding all question sentences from a webpage?
I am trying to extract some questions from a web site using BeautifulSoup, and want to use regular expression to get these questions from the web. Is my regular expression incorrect? And how can I combine soup.find_all with re.compile?
I have tried the following:
from bs4 import BeautifulSoup
import...
1
votes
2
answer
44
Views
PHP replace image src and add a new attribute in image tag from a string containing different html tags [duplicate]
This question already has an answer here:
How do you parse and process HTML/XML in PHP?
30 answers
I have a site where i get products description from database and decode html like this in PHP and display it on webpage frontend:
$data['description'] = html_entity_decode($product_info['description']...
1
votes
1
answer
46
Views
In DB2 SQL RegEx, how can a conditional replacement be done without CASE WHEN END..?
I have a DB2 v7r3 SQL SELECT statement with three instances of REGEXP_SUBSTR(), all with the same regex pattern string, each of which extract one of three groups.
I'd like to change the first SUBSTR to REGEXP_REPLACE() to do a conditional replacement if there's no match, to insert a default value s...
1
votes
3
answer
34
Views
Regex remove replace all whitespaces and commas with a plus sign
I have a query like this
2258, Hampton Street, Columbia, SC, USA
I need to replace all the white spaces and commas with a plus, so I do this:
query = query.replace(/,? /g, '+');
Problem is when there are multiple commas or whitespaces it doesn't work (or if there are whitespaces or commas at the end...
1
votes
2
answer
26
Views
Regex to match X digits or letters but not X letters in a row
I want to extract 7-char matches. Each character can be a digit or uppercase letter but the whole match can't be only uppercase letters. Example: let's say I have a test string like so:
I want this nr A7A3G1A but not this ANTENNA
So I should get A7A3G1A but not ANTENNA. A regex to match both would...
1
votes
4
answer
109
Views
How to write a regular expression that allows non-repeating underscores
I'm working on a homework assignment to use Flex to create a lexer. The last requirement I have to meet is:
The definition for the identifiers should be modified so that underscores can be included, however, consecutive underscores, leading and trailing underscores should not be permitted.
The give...
1
votes
2
answer
64
Views
And operator in regex?
I have a string with special marks in string and I want to delete them.
here is my string:
let message='Hello my name is /# Jane #/, if from /# company#/'. Could you please call me back .
now I'm trying to delete this /#---#/ marks
message.replace(/#/g, '')
but how add 'AND' operator in regex to de...
1
votes
2
answer
41
Views
How can I write a regex to pick repeating patterns in php from a file
With this string from a file with similar lines,
03/21/19 11:20 LOC3 UNA:
03/21/19 11:40 LOC2 IN: NEW BD PN VO LVA
03/21/19 11:50 LOC3 OFF:
03/21/19 12:20 LOC2 IN: OLD XD AB VO LVA
I need to capture the NEW, BD, PN, VO,LVA from lime 1, and OLD,XD,AB,VO,LVA in line 2 and so on, ignor...
1
votes
1
answer
40
Views
Allow only single type of mark in string
I'm trying to figure out, how to allow only single type of mark in string. For example if string inputStr contains different marks:
string inputStr = 'hello, how are you? ~ say something: what's up? hi... tell me. what?! ok: so,';
this way:
string outputStr = Regex.Replace(inputStr, @'[^\w\s]', '');...
1
votes
2
answer
49
Views
Regex clean up text where there's no text before/after a seperator
I am trying to clean up some text where there are separators with no text before/after it.
The 'types' are
3150779 | 3674-4 |Water Supply Plan
3637730 |
| 10903-155 | Layout 10903 DWG 155 29 M |
| 10903-155 | | Water Supply |
I understand [^\|]+ splits this but I want to get rid of the separator wh...
1
votes
1
answer
31
Views
Remove Whitespace from strings while Splitting by “,”
I need to identify the words after some keyword and before the symbols '=' OR ';'
I am using the below approach by splitting
String res = 'private String name1, name2, name3; ';
Matcher matcherWithCommar = Pattern
.compile('(?
1
votes
3
answer
46
Views
Make sed regex alternations follow left to right precedence?
I'm trying to use a regex to format some binary from xxd -b, but to demonstrate this simply I'll show you what I expect to happen:
Regex to delete: /1x|1.*/
Text: 1x21y3333333313333 -> 2
Where all occurrences of 1x are deleted, then everything starting at the first 1 that shows up should be deleted....
1
votes
3
answer
54
Views
How to split comma, semicolon and comma separated phrases with semicolons around?
How to split comma, semicolon and comma separated phrases with semicolons around where it should treat anything between semicolons with a comma as the delimiter, but also delimit just comma and semicolon all together in funcion?
String of words:
var words = 'apple;banana;apple, banana;fruit'
Regex f...
1
votes
3
answer
55
Views
extracting before digits before and after forward slash /
I have trouble with extracting the string before and after /.
x before_slash
[1] 'maximusa/b=(5/+1)' 'maximusa/b=(-4/1)' 'maximusa/b=(+3/-2)'
I tried some but no luck!
1
votes
1
answer
53
Views
Can't create unknown regex: $RE{some}{pattern}
I have a code here which throws the following error - 'Can't create unknown regex: $RE{some}{pattern} ' when I am running it using module Regexp::Common.
The objective of the code is to read a file,search for a string and execute 2 conditions. First condition: if the function timing is present for a...
1
votes
2
answer
37
Views
Split number thousand java
I want to split some String by adding some commas in it.
For example :
'1234' => '1,234'
'12345' => '12,345'
'123456' => '123,456'
'1234567' => '1,234,567'
'12345678' => '12,345,678'
'123456789' => '123,456,789'
I can have such a huge String like '123456789123456789123456789123456789123456789'
Curre...
1
votes
2
answer
54
Views
Fetch the values from a string with regular expression in bash
I have a string and I want to extract the needed values from this string, so I wrote some regex patterns, I searched in google and I find I have to use echo command and then store in a variable.
I tested my regex pattern with regex101.com and the pattern is correct but I don't know why not work in b...
1
votes
4
answer
332
Views
How to remove alphabets and extract numbers using regex in python?
How to remove alphabets and extract numbers using regex in python?
import re
l=['098765432123 M','123456789012']
s = re.findall(r'(?
1
votes
3
answer
106
Views
Perl extract group with lookbehind from different line
I've tried web search and have read several answers on stackexchange, still cannot grasp why command does not extract anything. At the end I want to extract group with lookbehind from different line, e.g. from
Code>TEST1besttest2false
1
votes
4
answer
72
Views
How to make a list of the findings in a text file by using Regular Expression with R?
I have to transform all the arguments in a text character vector into an easy-to-reference format: A list that has 3 column (presenter, time and text) by using R (Sorry I should've been clearer).
For example, presenter should be
# HARPER'S
time should be
# [Day 1, 9:00 A.M.]
and text should be the r...