问题
We will use below regex to get the digits before the words.
Example :
838123 someWord 8 someWord 12 someWord
(\d+)\s*someWord
But sometimes anything will come between Number and word.Please see the below example line.
Ex:
43434 of someword 12 anything someword 2323 new someword
How to get the exact digit before that word using regex?
Please give me your suggestions.
回答1:
Do this:
(\d+)[^\d]+some[wW]ord
You need to accept anything other than digits themselves.
Also I considered both w
and W
since your examples contained both.
Demo
回答2:
Presuming that "anything" does not include digits, you could use this regex:
(\d+)[^\d]+someWord
Demo on regex101
回答3:
One possible "missed corner case" from CinCout's answer is if the match for someWord
must be exact, e.g. if notsomeWord
and someWordNotThis
shouldn't be matched.
The following extension to that regular expression provides a way to address this:
(\d+)[^\d]*[^\w]some[wW]ord[^\w]
Explanation: The [^\w]
before or after the matcher for someWord
look for a "non-word character" before and after it - an end of the line also counts here. This could of course be made more complex/specific, depending on the exact requirements.
Demo
回答4:
You could try something like this:
(\d+)\s?([^\d]*)
(\d+) - get the digits
\s? - discard a possible space
([^\d]*) - get all chars that are not digits
You can see the test here
回答5:
first separated the some[wW]ord
, number
and space
with a pattern, then execute the second pattern on it
var pattern = @"\b(some[wW]ord|[\d]|\s)*\b";
var rgx = new Regex(pattern);
var sentence = "43434 of someword 12 anything someword 2323 new someword";
var result = string.Empty;
foreach (Match match in rgx.Matches(sentence)){
result += match.Value;
}
//output => result: 43434 someword 12 someword 2323 someword
var patternOnCorrectSentence = @"\b(\d+)\s*some[wW]ord*\b";
var rgxOnCorrectSentence = new Regex(patternOnCorrectSentence);
var resultOnCorrectSentence = new List<string>();
foreach (Match match in rgxOnCorrectSentence.Matches(result)){
resultOnCorrectSentence.Add(match.Value);
}
resultOnCorrectSentence.ForEach(Console.WriteLine);
Console.ReadKey();
When the first pattern is executed, the sentence will be as desired
43434 of someword 12 anything someword 2323 new someword
change:
43434 someword 12 someword 2323 someword
回答6:
But sometimes anything will come between Number and word.Please see the below example line.
Ex:
43434 of someword 12 anything someword 2323 new someword
try this
(\d+)(.*?)someword
Explained
\d+ - numbers
.*? - anything after numbers but minimum occurrence.
someword - exact match of somewhat
Demo
回答7:
Using \s*
will only match 0 or more whitespace characters.
You could use \D+
but it will also match newlines as it matches any char except a digit.
If you want to match the digits on the same line, you can add not matching a newline to a negated character class [^\d\r\n]
In your example, you use \d
, but if you only want to match 1 or more digits 0-9 you could use a character class [0-9]+
To prevent the digits and the word being part of a larger word you could make use of word boundaries \b
If you want to match the word in a case insensitive manner, you could use RegexOptions.IgnoreCase
or an inline modifier (?i)
(?i)\b([0-9]+)\b[^\d\r\n]*\bsomeword\b
See a .NET regex demo
回答8:
Use Named Match Captures (To get data use mtch.Groups["Value"].Value
... etc) to extract the information as needed.
(?<Value>\d+) -- Get the digits
(?<Other>.+?) -- Capture all text, but minimal (greedy) capture
(?<Key>someword) -- til the keyword here.
When the above is run (with IgnorePatternWhiteSpace
otherwise remove the comments and join the pattern to run it such as (?<Value>\d+)(?<Other>.+?)(?<Key>someword)
with no regex options) it gets the data for each Data/Key pairs and organizes each in a single match.
Result
Here is the result (for your second example) which are all contained in individual matches and their groups and captures provide in each match:
Match #0
[0]: 43434˽of˽someword
["Value"] → [1]: 43434
→1 Captures: 43434
["Other"] → [2]: ˽of˽
→2 Captures: ˽of˽
["Key"] → [3]: someword
→3 Captures: someword
Match #1
[0]: 12˽anything˽someword
["Value"] → [1]: 12
→1 Captures: 12
["Other"] → [2]: ˽anything˽
→2 Captures: ˽anything˽
["Key"] → [3]: someword
→3 Captures: someword
Match #2
[0]: 2323˽new˽someword
["Value"] → [1]: 2323
→1 Captures: 2323
["Other"] → [2]: ˽new˽
→2 Captures: ˽new˽
["Key"] → [3]: someword
→3 Captures: someword
Visually here is what is matched:
来源:https://stackoverflow.com/questions/59317088/how-to-get-the-digits-before-some-particular-word-using-regex-in-c