I have a string like the following
"blaa...blup..blaaa...bla."
Every part where there is more than one dot must be replaced by "_" but it must have the same amount as replaced chars.
The string should result in:
"bla___blup__blaaa___bla."
Notice that the last dot is not replaced as it has not other dots "connected".
I tried using following regex approach in powershell but I always get a legnth of 2 for the match regardless if there where 3 or more dots:
$string -replace '(.)\1+',("_"*'$&'.length)
Any ideas?
You can use the \G anchor to glue a match to the previous.
\.(?=\.)|\G(?!^)\.
\.(?=\.) match a period if another one is ahead.
|\G(?!^)\. or replace period if there was a previous match (but not start)
Replace with underscore. See demo at regexstorm
You can use the following pattern:
\.(?=\.)|(?<=\.)\.
And replace with _.
The pattern simply looks for either a period that is preceded by a period or a period which is followed by a period:
\.(?=\.) - Matches a period which is followed by a period
| - Or
(?<=\.)\. - Matches a period which is preceded by a period
See the online demo.
None of the languages and regex flavors I know allow you to evaluate the backreference numeric value "on the fly", you can only use it in the callback function. See Use a function in Powershell replace.
However, in this particular case, you can use the following regex:
((?=\.{2})|(?!^)\G)\.
And replace with _.
See the regex demo here.
And the explanation:
((?=\.{2})|(?!^)\G) - a boundary that either matches a location before 2 dots (with (?=\.{2})) or the end of the previous successful match (with (?!^)\G)
\. - a literal dot.
Related
I have a string and would like to match a part of it.
The string is Accept: multipart/mixedPrivacy: nonePAI: <sip:4168755400#1.1.1.238>From: <sip:4168755400#1.1.1.238>;tag=5430960946837208_c1b08.2.3.1602135087396.0_1237422_3895152To: <sip:4168755400#1.1.1.238>
I want to match PAI: <sip:4168755400#
the whitespace can be a word so i would like to use .* but if i used that it matches most of the string
The example on that link is showing what i'm matching if i use the whitespace instead of .*
(PAI: <sip:)((?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4})#
The example on that link is showing what i'm trying to achieve with .* but it should only match PAI: <sip:4168755400#
(PAI:.*<sip:)((?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4})#
I tried lookaround but failing.
Any idea?
thanks
Matching the single space can be updated by using a character class matching either a space or a word character and repeat that 1 or more times to match at least a single occurrence.
Note that you don't have to escape the spaces, and in both occasions you can use an optional character class matching either a space or hyphen [ -]?
If you want the match only, you can omit the 2 capturing groups if you want to.
(PAI:[ \w]+<sip:)((?:\([2-9]\d{2}\) ?|[2-9]\d{2}[ -]?)[2-9]\d{2}[- ]?\d{4})#
Regex demo
The regex should be like
PAI:.*?(<sip:.*?#)
Explanation:
PAI:.*? find the word PAI: and after the word it can be anything (.*) but ? is used to indicate that it should match as few as possible before it found the next expression.
(<sip:.*?#) capturing group that we want the result.
<sip:.*?# find <sip: and after the word it can be anything .*? before it found #.
Example
I have been trying to make a regex to match the text after settings. in the following string
GPIO.setup(settings.pic_taken_led , GPIO.OUT, initial=GPIO.LOW) IE (pic_taken_led)
I have so far come up with /(?<=settings)([.]\w+)/g but it matches with the period and I need it without the period. (.pic_taken_led).
I feel I am close but I can't remove the period from the capture expression.
You can use
(?<=settings\.)\w+
^^^^^^^^^^^^^^^
See the regex demo.
Details
(?<=settings\.) - a positive lookbehind that matches a location immediately preceded with settings. string
\w+ - one or more letters, digits, _s.
VSCode demo:
You are really close. Since you want to capture pic_taken_led, just capture the String after .
So your regex should be (?<=settings).(\w+)
I am sorry I can't formulate a good question:
This regex should find the word 'period' followed by a whitespace and one digit:
period.*(?=\s[0-9]{1})|alternative
If I input the line TEST 2019 to period 3.csv the regex matches period.
If I input the line TEST period 3 2019.csv the regex matches period 3.
My indtended match is period 3
You can se what I mean from this screenshot from regex101:
For now I have solved it with lookbehind positve like this:
(?<=period\s)[0-9]{1,4}|alternative
This matches the digit after 'period' and I can just add 'period' for my specific purpose. But I don't understand why I get different matches.
You don't need .* after period, so just remove it in from your regex and write it like this
period(?=\s[0-9]{1})|alternative
This matches period literally which is followed by a whitespace and a number (ensured by your positive look ahead). Also you really don't need to write {1} as that's be default and is redundant. Also if you don't want period to match partially in a larger text, use word boundary \b before it and change your regex to this,
\bperiod(?=\s[0-9])|alternative
Demo
Also, your look behind (?<=period\s)[0-9]{1,4}|alternative is not correct for matching the text period and indeed that look behind will just match the number which is preceded by period and one whitespace.
Check this Demo
Im using Visual Studio 2017 and in a long long text file Im searching for a particular function but unable to find
here's what the regex Im using
c\.CreateMap\<(\w)+\,\s+Address\>
and I want to in these
c.CreateMap<ClientAddress, Address>()
c.CreateMap<Responses.SiteAddress, Data.Address>()
and so on.
As soon as I add "Address" in the regex it stops matching any.
what am I doing wrong?
You can try this
c\.CreateMap\<\w+\.?\w+?\,\s*\w*?\.?Address\>
Explanation
c\.CreateMap\< - Matches c\.CreateMap\<.
\w+ - Matches any word character one or more time.
\.? - Matches '.' zero or one time.
\, - Matches ','.
\s* - Matches space zero or more time.
\w - Matches word character zero or more time.
\.? - Matches '.' zero or one time.
Address\> - Matches Address\>.
Demo
P.S- In case you also want to match something like this.
c.CreateMap<Responses.SiteAddress.abc, Data.Address.xyz>()
You can use this.
c\.CreateMap\<(\w+\.?\w+?)*\,\s*(?:\w*?\.?)*Address(\.\w*)?\>
Demo
Here is general regex I can suggest:
c\.CreateMap\<[\w.]+,\s+(?:[\w.]+\.)?Address\>\s*\(\s*\)
This will match any term with dots or word characters in the first position in the diamond. In the second, position, it will match Address, or some parent class names, followed by a dot separator, followed by Address.
Demo
Note that I also include the empty function call parentheses in the regex. As well, I allow for flexibility in the whitespace may appear after the diamond, or between the parentheses.
In your second example, you have extra dot which is not handled. Your regex needs little modification. Also, you don't need to escape < or > or , Use this,
c\.CreateMap<([\w.])+,\s+[\w.]*Address>
Demo
To match any of the functions on your question, you can use:
c\.CreateMap[^)]+\)
Regex Demo
Regex Explanation:
My regular expression lets in periods for some reason, how can I keep that from happening.
Rules:
4-15 characters
Any alphanumeric characters
Underscore as long as it's not first or last
[A-Za-z][A-Za-z0-9_]{3,14}
I don't want "bad.example" for work.
Edit: changed to 4-15 characters
Your regex matches example as a substring of bad.example. Use anchors to prevent that:
^[A-Za-z][A-Za-z0-9_]{1,12}[A-Za-z]$
Note that (like your regex) this regex also prevents digits from matching in the first and last position - if they should be allowed (as per your specs), just add 0-9 at the end of the character classes.
^[A-Za-z][A-Za-z0-9_]{3,14}$
try this
This will match any alphanumeric at the beginning and end. In the middle it will accept from one up to twelve alphanumerics including an underscore:
^[a-zA-Z\d]\w{1,12}[a-zA-Z\d]$
It does not match bad.example but matches only example as your regex allows a character from 4 to 15.See here.
http://regex101.com/r/xV4eL5/5
To prevent it you need to match the whole input and not make partial matches.Put a ^ start anchor and $ end anchor.
Use
\A[A-Za-z0-9][\w]{1,12}[A-Za-z0-9]\Z