Looking for a string with regex and delete the whole line - regex

I am trying to find in Textpad a character with regex (for example "#") and if it is found the whole line should be deleted. The # is not at the beginnen of the line nor at the end but somewehre in between and not connected to another word, number or charakter - it stands alone with a whitespace left and right, but of course the rest of the line contains words and numbers.
Example:
My first line
My second line with # hash
My third line# with hash
Result:
My first line
My third line# with hash
How could I accomplish that?

Let's break it down:
^ # Start of line
.* # any number of characters (except newline)
[ \t] # whitespace (tab or space)
\# # hashmark
[ \t] # whitespace (tab or space)
.* # any number of characters (except newline)
or, in a single line: ^.*[ \t]#[ \t].*

try this
^(.*[#].*)$
Debuggex Demo
or maybe
(?<=[\r\n^])(.*[#].*)(?=[\r\n$])
Debuggex Demo

EDIT: Changed to reflect the point by Tim
This
public static void main(String[] args){
Pattern p = Pattern.compile("^.*\\s+#\\s+.*$",Pattern.MULTILINE);
String[] values = {
"",
"###",
"a#",
"#a",
"ab",
"a#b",
"a # b\r\na b c"
};
for(String input: values){
Matcher m = p.matcher(input);
while(m.find()){
System.out.println(input.substring(m.start(),m.end()));
}
}
}
gives the output
a # b

Related

Regex Express Return All Chars before a '/' but if there are 2 '/' Return all before that

I have been trying to get a regex expression to return me the following in the following situations.
XX -> XX
XXX -> XXX
XX/XX -> XX
XX/XX/XX -> XX/XX
XXX/XXX/XX -> XXX/XXX
I had the following Regex, however they do no work.
^[^/]+ => https://regex101.com/r/xvCbNB/1
=========
([A-Z])\w+ => https://regex101.com/r/xvCbNB/2
They are close but are not there.
Any Help would be appreciated.
You want to get all text from the start till the last occurrence of a specific character or till the end of string if the character is missing.
Use
^(?:.*(?=\/)|.+)
See the regex demo and the regex graph:
Details
^ - start of string
(?:.*(?=\/)|.+) - a non-capturing group that matches either of the two alternatives, and if the first one matches first the second won't be tried:
.*(?=\/) - any 0+ chars other than line break chars, as many as possible upt to but excluding /
| - or
.+ - any 1+ chars other than line break chars, as many as possible.
It will be easier to use a replace here to match / followed by non-slash characters before end of line:
Search regex:
/[^/]*$
Replacement String:
""
Updated RegEx Demo 1
If you're looking for a regex match then use this regex:
^(.*?)(?:/[^/]*)?$
Updated RegEx Demo 2
Any special reason it has to be a regular expression? How about just splitting the string at the slashes, remove the last item and rejoin:
function removeItemAfterLastSlash(string) {
const list = string.split(/\//);
if (list.length == 1) [
return string;
}
list.pop();
return list.join("/");
}
Or look for the last slash an remove it:
function removeItemAfterLastSlash(string) {
const index = string.lastIndexOf("/");
if (index === -1) {
return string;
}
return string.splice(0, index);
}

Remove given string from both start and end of a word

Data :
col 1
AL GHAITHA
AL ASEEL
EMARAT AL
LOREAL
ISLAND CORAL
My code :
def remove_words(df, col, letters):
regular_expression = '^' + '|'.join(letters)
df[col] = df[col].apply(lambda x: re.sub(regular_expression, "", x))
Desired output :
col 1
GHAITHA
ASEEL
EMARAT
LOREAL
ISLAND CORAL
SUNRISE
Function call :
letters = ['AL','SUPERMARKET']
remove_words(df=df col='col 1',letters=remove_letters)
Basically, i wanted remove the letters provided either at the start or end. ( note : it should be seperate string)
Fog eg : "EMARAT AL" should become "EMARAT"
Note "LOREAL" should not become "LORE"
Code to build the df :
raw_data = {'col1': ['AL GHAITHA', 'AL ASEEL', 'EMARAT AL', 'LOREAL UAE',
'ISLAND CORAL','SUNRISE SUPERMARKET']
}
df = pd.DataFrame(raw_data)
You may use
pattern = r'^{0}\b|\b{0}$'.format("|".join(map(re.escape, letters)))
df['col 1'] = df['col 1'].str.replace(pattern, r'\1').str.strip()
The (?s)^{0}\b|(.*)\b{0}$'.format("|".join(map(re.escape, letters)) pattern will create a pattern like (?s)^word\b|(.*)\bword$ and it will match word as a whole word at the start and end of the string.
When checking the word at the end of the string, the whole text before it will be captured into Group 1, hence the replacement pattern contains the \1 placeholder to restore that text in the resulting string.
If your letters list contains items only composed with word chars you may omit map with re.escape, replace map(re.escape, letters) with letters.
The .str.strip() will remove any resulting leading/trailing whitespaces.
See the regex demo.

Remove the text before second comma ('',") String replace pattern

how can we remove the text before the line that start's with second comma(line 5 in the example),how can i do that using regex?
example :
,
abc,xyz,ggg,nrmr
cde,jjj,kkkk,iiii,tem,posting
234,mm/dd/yy
,
454654,output2,sample
45646,output1,non-sample
16546,225.02
ABC,2.98
expected :
454654,output2,sample
45646,output1,non-sample
16546,225.02
ABC,2.98
It seems you may use
val s = """,
abc,xyz,ggg,nrmr
cde,jjj,kkkk,iiii,tem,posting
234,mm/dd/yy
,
454654,output2,sample
45646,output1,non-sample
16546,225.02
ABC,2.98"""
val res = s.replaceFirst("(?sm)\\A(.*?^,$){2}", "").trim()
println(res)
// =>
// 454654,output2,sample
// 45646,output1,non-sample
// 16546,225.02
// ABC,2.98
See the Scala demo.
Pattern details:
(?sm) - s enables . to match any char in the string including newlines, and m makes ^ and $ match start/end of line respectively
\\A - the start of string
(.*?^,$){2} - 2 occurrences of:
.*? - any 0+ chars as few as possible up to the leftmost
^,$ - line that only contains ,.

Regular expression in Flex

I want to check if the string is not empty (having whitespaces only also counts as empty). How to compose the regular expression in actionscript?
The pattern should be something like /^\s*$/ (for a single line string); ^ and $ represent the start and end of the line and \s* means match zero or more whitespace characters. For example:
var s:String = /* ... */;
var allWhitespaceOrEmpty:RegExp = /^\s*$/;
if (allWhitespaceOrEmpty.test(s))
{
// is empty or all whitespace
}
else
{
// is non-empty with at least 1 non-whitespace char
}
Perhaps a simpler way as commenter Alexander Farber points out is to check for any character except a whitespace character, which is matched by \S in regex:
var nonWhitespaceChar:RegExp = /\S/;
if (nonWhitespaceChar.test(s))
{
// is non-empty with at least 1 non-whitespace char
}

Regex that matches specific spaces

I've been trying to do this Regex for a while now. I'd like to create one that matches all the spaces of a text, except those in literal string.
Exemple:
123 Foo "String with spaces"
Space between 123 and Foo would match, as well as the one between Foo and "String with spaces", but only those two.
Thanks
A common, simple strategy for this is to count the number of quotes leading up to your location in the string. If the count is odd, you are inside a quoted string; if the amount is even, you are outside a quoted string. I can't think of a way to do this in regular expressions, but you could use this strategy to filter the results.
You could use re.findall to match either a string or a space and then afterwards inspect the matches:
import re
hits = re.findall("\"(?:\\\\.|[^\\\"])*\"|[ ]", 'foo bar baz "another\\" test\" and done')
for h in hits:
print "found: [%s]" % h
yields:
found: [ ]
found: [ ]
found: [ ]
found: ["another\" test"]
found: [ ]
found: [ ]
A short explanation:
" # match a double quote
(?: # start non-capture group 1
\\\\. # match a backslash followed by any character (except line breaks)
| # OR
[^\\\"] # match any character except a '\' and '"'
)* # end non-capture group 1 and repeat it zero or more times
" # match a double quote
| # OR
[ ] # match a single space
If this ->123 Foo "String with spaces" <- is your structure for a line that is to say text followed by a quoted text you could create 2 groups the quoted and the unquoted text and an tackle them separately.
ex.regex -> (.*)(".*") where $1 should contain ->123 Foo <- and $2 ->"String with spaces"<-
java example.
String aux = "123 Foo \"String with spaces\"";
String regex = "(.*)(\".*\")";
String unquoted = aux.replaceAll(regex, "$1").replace(" ", "");
String quoted = aux.replaceAll(regex, "$2");
System.out.println(unquoted+quoted);
javascript example.
<SCRIPT LANGUAGE="JavaScript">
<!--
str='1 23 Foo \"String with spaces\"';
re = new RegExp('(.*)(".*")') ;
var quoted = str.replace(re, "$1");
var unquoted = str.replace(re, "$2");
document.write (quoted.split(' ').join('')+unquoted);
// -->
</SCRIPT>