I want to replace all instances of . with , in Word.
I want to find ([0-9]).([0-9]) and replace it with \1,\2. But it is changing 0.72 to 07,2 when I want 0,72.
Where am I going wrong?
To match the . character literally you should add a \ character before the . like this
([0-9]+)\.([0-9]+)
And to allow many Number characters before and after the . you can add + sign after each list of character ([0-9])
Related
I apologize because I know this has been asked so many times. Yet, I cannot find an answer that works for me! It seems most answers seem framed in a particular language like Java or php. I'm trying to make this work in "pure" regex format at the termimal (mac) line. This bit of code works but I'd like to find special chars and replace with underscores. As it is right now, the regex replaces with nothing. Any idea how I can specify the replace value?
find . -type f|while read f;do b=${f##*/};mv "$f" "${f%/*}/${b//[^[:alnum:]_.]}";done
You can specify the replacement after another /:
"${f%/*}/${b//[^[:alnum:]_.]/_}"
# ↑↑
Here, ${b//[^[:alnum:]_.]/_} means that any single char other than a letter, digit, underscore and dot will be replaced with a _ char.
If you need to replace chunks of one or more special chars with a single underscore use
shopt -s extglob
"${f%/*}/${b//+([^[:alnum:]_.])/_}"
shopt -u extglob
I wanted to know the regex expression that detects names starting with #. For eg, in the sentence "Hi #Steve Rogers, how are you?", I want to extract out #Steve Rogers using regex. I tried using Pattern.compile("#\\s*(\\w+)").matcher(text), but only "#Steve" get detected. What else should I use.??
Thanks
Try (#[\w\s]+)
It will only capture word and spaces after the #
See example at https://regex101.com/r/4Pv9bu/1
If you don't want to match an # sign followed by a space only like # and if there can be more than a single word after it:
(?<!\S)#\w+(?:\h+\w+)?
Explanation
(?<!\S) Assert a whitespace boundary to the left
# Match literally
\w+ Match 1+ word characters
(?:\s+\w+)? Optionally match 1+ horizontal whitespace chars and 1+ word chars
Regex demo
In Java
String regex = "(?<!\\S)#\\w+(?:\\h+\\w+)?";
I have one string
"8.53" I want my resulting string "853"
I have tried
the following code
tr|.||;
but its not replacing its giving 8.53 only .
I have tried another way using
tr|.|NULL|;
but its giving 8N53 can anyone please suggest me how to use tr to replace a character with NULL.
Thanks
You need to specify the d modifier to delete chars with no corresponding char:
tr/.//d;
Or you could use the (slower but more familiar) substitution operator:
s/\.//g;
You don't want tr because that transliterates characters from the 1st list with the corresponding character in the 2nd list (which was N in your example since that was the first character). You'll want the substitution operator.
my $var = "8.53";
$var =~ s/\.//;
print $var;
Add the g flag if there are multiple instances you want to replace (s/\.//g).
I have tried [0-9] and checked the use wildcard box but it replaces the individual numbers with the literal [0-9] string. How do I replace with the number it found plus a character?
Backreferences. Your unspecified environment may or may not support them, but if it does, you would:
replace \([0-9]*\)
with \1 <then, whatever the character you want is>
I am trying to write a sed script to convert LaTeX coded tables into tab delimited tables.
To do this I need to convert & into \t and strip out anything that is preceded by \.
This is what I have so far:
s/&/\t/g
s/\*/" "/g
The first line works as intended. In the second line I try to replace \ followed by anything with a space but it doesn't alter the lines with \ in them.
Any suggestions are appreciated. Also, can you briefly explain what suggested scripts "say"? I am new to sed and that really helps with the learning process!
Thanks
Assuming you're running this as a sed script, and not directly on the command line:
s/\\.*/ /g
Explanation:
\\ - double backslash to match a literal backslash (a single \ is interpreted as "escape the following character", followed by a .* (. - match any single character, * - arbitrarily many times).
You need to escape the backslash as it is a special character.
If you want to denote "any character" you need to use . (a period)
the second expression should be:
s/\\.//g
I hope I understood your intention and you want to strip the character after the backslash,
if you want to delete all the characters in the line after the backslash add a star (*)
after the period.