How to replace any_string#a.net to any_string#b.com using RegEx?
I want to strip the #a.net and replace it with #b.com
I've tried
(.*#a.net)
but the $1 is showing all the string.
So when i try to replace it, it became
any_string#a.net#b.com
And can someone point me to a nice tutorial regarding RegEx?
The () indicates the capture group. Put the parts of the expression you don't want to capture outside the parens:
(.*)#a.net
A great site to play around with regular expressions is http://refiddle.com/.
I fiddled this problem already.
You can use
\b#[a-zA-Z].net\b
\b to set word boundaries before #
# matches the character # literally
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
. matches any character (except newline)
net matches the characters net literally (case sensitive)
\b word boundary
The above regex will capture the given characters literally which you can replace using #b.com
And of you simply want to capture only #a.net than you can simply use
\b#a.net\b
Regex Demo
Related
I am having a hard time coming up with a regex to match a specific case:
This can be matched:
any-dashed-strings
this-can-be-matched-even-though-its-big
This cannot be matched:
strings starting with elem- or asdf- or a single -
elem-this-cannot-be-matched
asdf-this-cannot-be-matched
-
So far what I came up with is:
/\b(?!elem-|asdf-)([\w\-]+)\b/
But I keep matching a single - and the whole -this-cannot-be-matched suffix. I cannot figure it out how to not only ignore a character present inside the matching character class conditionally, and not matching anything else if a suffix is found
I am currently working with the Oniguruma engine (Ruby 1.9+/PHP multi-byte string module).
If possible, please elaborate on the solution. Thanks a lot!
If a lookbehind is supported, you can assert a whitespace boundary to the left, and make the alternation for both words without the hyphen optional.
(?<!\S)(?!(?:elem|asdf)?-)[\w-]+\b
Explanation
(?<!\S) Assert a whitespace boundary to the left
(?! Negative lookahead, assert the directly to the right is not
(?:elem|asdf)?- Optionally match elem or asdf followed by -
) Close the lookahead
[\w-]+ Match 1+ word chars or -
\b A word boundary
See a regex demo.
Or a version with a capture group and without a lookbehind:
(?:\s|^)(?!(?:elem|asdf)?-)([\w-]+)\b
See another regex demo.
There are a thousand regular expression questions on SO, so I apologize if this is already covered. I did look first.
I have string:
Name Subname 11X22 88X620 AB33(20) YA5619 77,66
I need to capture this string: YA5619
What I am doing is just finding AB33(20) and after this I am capturing until first white space. But AB33(20) can be AB-33(20) or AB33(-20) or AB33(-1).
My preg_match regex is: (?<=\bAB\d{2}\(\d{2}\)\s).+?(?=\s)
Why I am getting error when I change from \d{2} to \d+?
For final result I was thinking this regix will work but no:
(?<=\bAB-?\d+\(-?\d+\)\s).+?(?=\s)
Any ideas what I am doing wrong?
With most regex flavors, lookbehind needs to evaluate to a fixed-length sequence, so you can't use variable quantifiers like * or + or even {1,2}.
Instead of using lookaround, you can simply match your marker pattern and then forget it with \K.
AB-?\d+(?:\(-?\d+\))? \K[^ ]+
demo: https://regex101.com/r/8XXngH/1
It depends on the language. If it is in .NET for example, it matches due to the various length in the lookbehind.
Another solution might be to use a character class and add the character you would allow to match. Then match a whitespace character and capture in a group matching \S+ which matches 1+ times not a whitespace character.
\bAB[()\d-]+\s\K\S+
Explanation
\bAB Match literally prepended with word boundary to prevent AB being part of a larger match.
[()\d-]+ Match 1+ times any of the listed character in the character class
\s Match a whitespace char (or \s+ to match 1 or more)
\K Reset the starting point of the reported match( Forget what was matched)
\S+ Match in a group 1+ times not a whitespace character
Regex demo | Php demo
I'm trying to write a small regex to extract the text before an optional (.
I have this:
^(.*)[\(.*]|$
But its not working for some reason. Doesn't seem to make it to the $ if there is no ( present.
Any help would be much appreciated
Cheers
Your regex will either capture 0+ times any character in a capturing group (.*) followed by a character class matching one of the listed characters [\(.*], or it will match an empty string due to the alternation |$.
If the first part of the alternation does not match a character from the character class at the end, you will not have a match.
You could use a negated character class to match not a ( from the start of the string:
^[^(]+
I have tried to find the solution of this problem, but I still can't get the correct answer. Therefore, I decided to ask you all here for help.
I have some text :
CommentTimestamps:true,showVODCommentTimestamps:false,enableVODStreamingComments:false,enablePinLiveComments:false,enableFacecastAnimatedComments:false,permalink:"1",isViewerTheOwner:false,isLiveAudio:false,mentionsinput:{inputComponent:{__m:"LegacyMentionsInput.react"}},monitorHeight:false,viewoptionstypeobjects:null,viewoptionstypeobjectsorder:null,addcommentautoflip:true,autoplayLiveVODComments:true,disableCSSHiding:true,feedbackMode:"none",instanceid:"u_0_w",lazyFetch:true,numLazyComments:2,pagesize:50,postViewCount:"78,762",shortenTimestamp:true,showaddcomment:true,showshares:true,totalPosts:1,viewCount:"78,762",viewCountReduced:"78K"},{comments:[],pinnedcomments:[],profiles:{},actions:[],commentlists:{comments:{"1":{filtered:{range:{offset:32,length:0},values:[],count:32,clienthasall:false}}},replies:null},featuredcommentlists:{comments:null,replies:null},featuredcommentids:null,servertime:1492916773,feedb.........`
What I want to get is only : postViewCount:"78,762"
I have tried using [^(postViewCount\b.......)] but it is not what I want to get.
This should do it
(postViewCount:\"\d{2}\,\d{3}\")
https://regex101.com/r/9JENH0/1
postViewCount: matches the characters postViewCount: literally (case sensitive)
\" matches the character " literally (case sensitive)
\d{2} matches a digit (equal to [0-9]) {2} Quantifier — Matches exactly 2 times
\, matches the character , literally (case sensitive)
Now if the count is one million or larger then use (postViewCount:"(?:.*?)")
Regex: postViewCount:"[^"]+"
1. postViewCount:" will match postViewCount:"
2. [^"]+ match all till "
Regex demo
try to match -
.*(postViewCount:"[0-9,]*").*
and replace it with catched group that is \1
Regex demo
I'd like some help with regular expressions because I'm not really familiar with.
So far, I have created the following regex:
/\b(?<![\#\-\/\>])literal(?![\<\'\"])\b/i
As https://regex101.com/ states:
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
Negative Lookbehind (?])
Assert that the Regex below does not match
Match a single character present in the list below [#-/>]
# matches the character # literally (case insensitive)
- matches the character - literally (case insensitive)
/ matches the character / literally (case insensitive)
> matches the character > literally (case insensitive)
literal matches the characters literal literally (case insensitive)
Negative Lookahead (?![\<\'\"])
Assert that the Regex below does not match
Match a single character present in the list below [\<\'\"]
\< matches the character < literally (case insensitive)
\' matches the character ' literally (case insensitive)
\" matches the character " literally (case insensitive)
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of
[a-zA-Z])
I want to add two exceptions to this matching rule. 1) if the ">" is preceded by "p", that is for example a <p> starting tag, to match the literal only. 2) Also the literal should only be matched when < is follwed by /p, that is for example a </p> closing tag.
How can achieve this ?
Example: only the bold ones should match.
<p>
**Literal** in computer science is a
<a href='http://www.google.com/something/literal#literal'>literal</a>
for representing a fixed value in source code. Almost all programming
<a href='http://www.google.com/something/else-literal#literal'>languages</a>
have notations for atomic values such as integers, floating-point
numbers, and strings, and usually for booleans and characters; some
also have notations for elements of enumerated types and compound
values such as arrays, records, and objects. An anonymous function
is a **literal** for the function type which is **LITERAL**
</p>
I know I have over-complicated things, but the situation is complicated itself and I think I have no other way.
If the text you're searching is just text mixed with some <a> tags, then you can simplify the < and > parts of the lookarounds, and give a specific string that it shouldn't be followed by: </a>.
/\b(?<![-#\/])literal(?!<\/a>)\b/i
Regex101 Demo