I trying to have a regular expression which is finding between two words but those words are not certain one.
JavaScript
x
2
1
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303
2
This is my text. I’m trying to find the word between Soyadı
and Sınıfı
, in this case ERTANĞA
, but the word Sınıfı
also can be no
, numara
or any number. This is what I did.
JavaScript
1
2
1
soyad[ıi](.*)S[ıi]n[ıi]f[ıi]|no|numara|[0-9]
2
[ıi]
is for Turkish character issue, don’t mind that.
Advertisement
Answer
You can use a single capture group to get the word ERTANĞA
, keep the character class [ıi]
instead of using an alternation for (ı|i)
and group the alternatives at the end of the pattern using a non capture group (?:
JavaScript
1
2
1
soyad[ıi](.+?)(?:S[ıi]n[ıi]f[ıi]|n(?:o|umara)|[0-9])
2
soyad[ıi]
Matchsoyadı
orsoyadi
(.+?)
Capture group 1, match 1 or more chars as least as possible(?:
Non capture groupS[ıi]n[ıi]f[ıi]
MatchS
and thenı
ori
etc..|
Orn(?:o|umara)
Match eitherno
ornumara
|
Or[0-9]
Match a digit 0-9
)
Close non capture group
Note that you don’t need the /m
flag as there are no anchors in the pattern.
JavaScript
1
3
1
const regex = /soyad[ıi](.+?)(?:S[ıi]n[ıi]f[ıi]|n(?:o|umara)|[0-9])/gi;
2
const str = "2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303n";
3
console.log(Array.from(str.matchAll(regex), m => m[1]));